Rajendra Dharmkar has Published 453 Articles

How can I apply an offset on the current time in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 04-Nov-2023 01:13:51

3K+ Views

Whenever you want to add or subtract(apply an offset) to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. The timedelta constructor has the following function signature − datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) ... Read More

How to insert date object in MySQL using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 04-Nov-2023 01:08:29

13K+ Views

To insert a date in a MySQL database, you need to have a column of Type date or datetime in your table. Once you have that, you'll need to convert your date in a string format before inserting it to your database. To do this, you can use the datetime ... Read More

How to write a case insensitive Python regular expression without re.compile?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 03-Nov-2023 02:49:54

9K+ Views

We can pass re.IGNORECASE to the flags parameter of search, match, or sub −Example import re print (re.search('bush', 'BuSh', re.IGNORECASE)) print (re.match('bush', 'BuSh', re.IGNORECASE)) print (re.sub('bush', 'xxxx', 'Bushmeat', flags=re.IGNORECASE))Output xxxxmeat

How do we use re.finditer() method in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 14:22:39

14K+ Views

According to Python docs, re.finditer(pattern,  string,  flags=0)Return an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result. The following code shows the use of re.finditer() method ... Read More

Why do we use re.compile() method in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 14:06:21

11K+ Views

The re.compile() methodre.compile(pattern, repl, string): We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it. Example import re pattern=re.compile('TP') result=pattern.findall('TP Tutorialspoint TP') print result result2=pattern.findall('TP is most popular tutorials site of India') ... Read More

How to extract floating number from text using Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:27:48

4K+ Views

The following code extracts floating numbers from given text/string using Python regex. Example import re s = "Sound Level: -11.7 db or 15.2 or 8 db" result = re.findall(r"[-+]?\d*\.\d+|\d+", s) print result OutputThis gives the output['-11.7', '15.2', '8']

How to simulate scanf() method using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:26:24

5K+ Views

According to Python documentationPython does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf() format strings. The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions.scanf() TokenRegular Expression%c.%5c.{5}%d[-+]?\d+%e, %E, %f, %g[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?%i[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)%o[-+]?[0-7]+%s\S+%u\d+%x, %X[-+]?(0[xX])?[\dA-Fa-f]+To extract the filename and ... Read More

How to write a regular expression to match either a or b in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:22:22

3K+ Views

The following code uses a regular expression '(a|b)' to match a or b in the given Python string.We are also using the flag re.I to ignore case of a or b while matching. Example import re s = 'Bank of Baroda' print(re.findall(r'(a|b)', s, re.I))OutputThis gives the output['B', 'a', 'B', ... Read More

How to subtract Python timedelta from date in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:16:07

5K+ Views

You can subtract a day from a Python date using the timedelta object. You need to create a timedelta object with the amount of time you want to subtract. Then subtract it from the date. Example from datetime import datetime from datetime import timedelta today = datetime.today() yesterday = today ... Read More

How to convert unix timestamp string to readable date in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:13:20

3K+ Views

You can use the fromtimestamp() function from the datetime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp.  Exmaple import datetime timestamp = datetime.datetime.fromtimestamp(1500000000) print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))OutputThis will give the output −2017-07-14 08:10:00Read More

1 2 3 4 5 ... 46 Next
Advertisements