Categories
Python Answers

How to read large text files line by line, without loading it into memory with Python?

Sometimes, we want to read large text files line by line, without loading it into memory with Python.

In this article, we’ll look at how to read large text files line by line, without loading it into memory with Python.

How to read large text files line by line, without loading it into memory with Python?

To read large text files line by line, without loading it into memory with Python, we can use with with open.

For instance, we write

with open("log.txt") as infile:
    for line in infile:
        do_something_with(line)

to call open to open log.txt with with.

Then we do whatever we want to do with the file in the with block.

Using with will automatically clear the file from memory when it’s not used.

Conclusion

To read large text files line by line, without loading it into memory with Python, we can use with with open.

Categories
Python Answers

How to determine if a Python shell is executing in 32bit or 64bit?

Sometimes, we want to determine if a Python shell is executing in 32bit or 64bit.

In this article, we’ll look at how to determine if a Python shell is executing in 32bit or 64bit.

How to determine if a Python shell is executing in 32bit or 64bit?

To determine if a Python shell is executing in 32bit or 64bit, we can use the platform module.

For instance, we write

import platform

print(platform.architecture()[0])

to call platform.architecture and get the first part from the list to either display '32bit' if it’s running on a 32 bit shell or '64bit' if it’s running on a 64 bit shell.

Conclusion

To determine if a Python shell is executing in 32bit or 64bit, we can use the platform module.

Categories
Python Answers

How to sort a list of tuples by 2nd item with Python?

Sometimes, we want to sort a list of tuples by 2nd item with Python.

In this article, we’ll look at how to sort a list of tuples by 2nd item with Python.

How to sort a list of tuples by 2nd item with Python?

To sort a list of tuples by 2nd item with Python, we can use the sorted function.

For instance, we write

l = sorted([("abc", 121), ("abc", 231), ("abc", 148), ("abc", 221)], key=lambda x: x[1])

to call sorted with a list and the key argument set to a function that returns the item to sort by in the tuple.

And then we assign the returned sorted list to l.

Conclusion

To sort a list of tuples by 2nd item with Python, we can use the sorted function.

Categories
Python Answers

How to extract text from HTML file using Python?

Sometimes, we want to extract text from HTML file using Python.

In this article, we’ll look at how to extract text from HTML file using Python.

How to extract text from HTML file using Python?

To extract text from HTML file using Python, we can use BeautifulSoup.

For instance, we write

from bs4 import BeautifulSoup

clean_text = ' '.join(BeautifulSoup(some_html_string, "html.parser").stripped_strings)

to create a BeautifulSoup object with some_html_string and 'html.parser'.

Then we get the stripped_strings property from the object to get a list of strings extracted from some_html_string.

Next, we call join to join the strings together with a space.

Conclusion

To extract text from HTML file using Python, we can use BeautifulSoup.

Categories
Python Answers

How to get the last day of the month with Python?

Sometimes, we want to get the last day of the month with Python.

In this article, we’ll look at how to get the last day of the month with Python.

How to get the last day of the month with Python?

To get the last day of the month with Python, we call the calendar.monthrange method.

For instance, we write

import calendar

d =  calendar.monthrange(2022, 1)

to get the last day of January 2022 by calling calendar.monthrange with the year and month.

Conclusion

To get the last day of the month with Python, we call the calendar.monthrange method.