Rajendra Dharmkar has Published 189 Articles

How to define attributes of a class in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 10:17:04

304 Views

Attributes of a classEverything, almost everything in Python is an object. Every object has attributes and methods. Thus attributes are very fundamental in Python. A class is a construct which is a collection of similar objects. A class also has attributes. There will be a difference between the class attributes ... Read More

How to read and write unicode (UTF-8) files in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:59:19

29K+ Views

The io module is now recommended and is compatible with Python 3's open syntax: The following code is used to read and write to unicode(UTF-8) files in PythonExampleimport io with io.open(filename, 'r', encoding='utf8') as f:     text = f.read() # process Unicode text with io.open(filename, 'w', encoding='utf8') as f: ... Read More

How to write a Python regular expression to match multiple words anywhere?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 05:31:13

3K+ Views

The following code using Python regex matches the given multiple words in the given stringExampleimport re s = "These are roses and lilies and orchids, but not marigolds or .." r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X) print r.findall(s)OutputThis gives the output['roses', 'orchids', 'marigolds']Read More

How to find and replace within a text file using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 12:13:43

434 Views

The following code does the replacement in the given text file. After the replacement, the text is written to a new text file 'bar.txt'Examplef1 = open('foo.txt', 'r') f2 = open('bar.txt', 'w') for line in f1:     print line     f2.write(line.replace('Poetry', 'Prose')) f2 = open('bar.txt', 'r') for line in f2: ... Read More

What are Python modules for date manipulation?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 09:46:01

269 Views

There are many modules available in both the standard library and the PiPy repository for date manipulation. The most popular among these libraries are the following(in no particular order) −datetime (Standard library) − The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While ... Read More

How to get the Python date object for last Wednesday?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 09:33:33

938 Views

You can get the Python date object for last wednesday using some Python date math. Whatever the day of the week it is today, subtracting 2 from it and taking the modulus of the result by 7 will give us how back was wedenesday. examplefrom datetime import date from datetime import ... Read More

How to get the timing Execution Speed of Python Code?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:44:46

2K+ Views

To measure time of a program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give the ... Read More

How to find if 24 hrs have passed between datetimes in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:44:15

3K+ Views

To find out if 24 hrs have passed between datetimes in Python, you will need to do some date math in Python. So if you have 2 datetime objects, you'll have to subtract them and then take the timedelta object you get as a result and use if for comparision. ... Read More

How to compare Python string formatting: % with .format?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 07:33:31

253 Views

% can either take a variable or a tuple. So you'd have to be very explicit about what you want it to do. For example, if you try formatting such that −Examplemy_tuple = (1, 2, 3) "My tuple: %s" % my_tuple You'd expect it to give the output: My tuple: ... Read More

How to create a unique directory name using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 04:56:12

1K+ Views

You can use the tempfile module to create a unique temporary directory in the most secure manner possible. There are no race conditions in the directory’s creation. The directory is readable, writable and searchable only by the creating user ID. Note that the user of mkdtemp() is responsible for deleting ... Read More

Advertisements