Categories
Python Answers

How to open multiple files using “with open” in Python?

Sometimes, we want to open multiple files using "with open" in Python.

In this article, we’ll look at how to open multiple files using "with open" in Python.

How to open multiple files using "with open" in Python?

To open multiple files using "with open" in Python, we can call open multiple times.

For instance, we write

with open('a', 'w') as a, open('b', 'w') as b:
    do_something()

to open files a and b with open and with.

Then we can do whatever we want with them inside the block.

Conclusion

To open multiple files using "with open" in Python, we can call open multiple times.

Categories
Python Answers

How to get the ASCII value of a character with Python?

Sometimes, we want to get the ASCII value of a character with Python.

In this article, we’ll look at how to get the ASCII value of a character with Python.

How to get the ASCII value of a character with Python?

To get the ASCII value of a character with Python, we can use the ord function.

For instance, we write

n = ord('a')

to return the ASCII value of 'a' with ord.

Conclusion

To get the ASCII value of a character with Python, we can use the ord function.

Categories
Python Answers

How to convert a datetime object to milliseconds since epoch (unix time) in Python?

Sometimes, we want to convert a datetime object to milliseconds since epoch (unix time) in Python.

In this article, we’ll look at how to convert a datetime object to milliseconds since epoch (unix time) in Python.

How to convert a datetime object to milliseconds since epoch (unix time) in Python?

To convert a datetime object to milliseconds since epoch (unix time) in Python, we can get a timedelta object by subtracting the datetime object by the datetime object at epoch.

For instance, we write

import datetime

epoch = datetime.datetime.utcfromtimestamp(0)

def unix_time_millis(dt):
    return (dt - epoch).total_seconds() * 1000.0

to create the unix_time_millis to subtract datetime dt with epoch to get a timedelta object with their difference.

Then we call total_seconds to seconds between dt and epoch.

Finally we multiply that by 1000 to get the difference in milliseconds.

Conclusion

To convert a datetime object to milliseconds since epoch (unix time) in Python, we can get a timedelta object by subtracting the datetime object by the datetime object at epoch.

Categories
Python Answers

How to extract first item of each sublist with Python?

Sometimes, we want to extract first item of each sublist with Python.

In this article, we’ll look at how to extract first item of each sublist with Python.

How to extract first item of each sublist with Python?

To extract first item of each sublist with Python, we can use list comprehension.

For instance, we write

lst = [["a", "b", "c"], [1, 2, 3], ["x", "y", "z"]]
lst2 = [item[0] for item in lst]

to create a list lst2 that has the first item from each nested list in lst in the list.

We use item[0] to get the item.

Conclusion

To extract first item of each sublist with Python, we can use list comprehension.

Categories
Python Answers

How to plot separate Pandas DataFrames as subplots?

Sometimes, we want to plot separate Pandas DataFrames as subplots.

In this article, we’ll look at how to plot separate Pandas DataFrames as subplots.

How to plot separate Pandas DataFrames as subplots?

To plot separate Pandas DataFrames as subplots, we can use the plot method.

For instance, we write

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=2)

df1.plot(ax=axes[0, 0])
df2.plot(ax=axes[0, 1])

to call plt.subplots to create a page with subplots.

Then we call plot on each dataframe with the ax argument set to the location we want to put the subplot of the dataframe.

Conclusion

To plot separate Pandas DataFrames as subplots, we can use the plot method.