Categories
Python Answers

How to parse HTML using Python?

Sometimes, we want to parse HTML using Python.

In this article, we’ll look at how to parse HTML using Python.

How to parse HTML using Python?

To parse HTML using Python, we can use Beautiful Soup.

For instance, we write

try: 
    from BeautifulSoup import BeautifulSoup
except ImportError:
    from bs4 import BeautifulSoup

# ...

parsed_html = BeautifulSoup(html)
print(parsed_html.body.find('div', attrs={'class':'container'}).text)

to create a BeautifulSoup object with the html HTML string to parse it into an object.

Then we call parsed_html.body.find with 'div‘ and the attr dict to find the div with the container class.

And we return its text content with text.

Conclusion

To parse HTML using Python, we can use Beautiful Soup.

Categories
Python Answers

How to sort a list by multiple attributes with Python?

Sometimes, we want to sort a list by multiple attributes with Python.

In this article, we’ll look at how to sort a list by multiple attributes with Python.

How to sort a list by multiple attributes with Python?

To sort a list by multiple attributes with Python, we can use the sorted function with the operator.itemgetter method.

For instance, we write

import operator

s = sorted(s, key = operator.itemgetter(1, 2))

to call sorted with list s and the key set to the function returned by operator.itemgetter(1, 2) to get the values with keys 1 and 2 from each entry in s.

Conclusion

To sort a list by multiple attributes with Python, we can use the sorted function with the operator.itemgetter method.

Categories
Python Answers

How to pretty print a JSON file with Python?

Sometimes, we want to pretty print a JSON file with Python.

In this article, we’ll look at how to pretty print a JSON file with Python.

How to pretty print a JSON file with Python?

To pretty print a JSON file with Python, we can use the json.dumps method with the indent argument.

For instance, we write

import json

your_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
parsed = json.loads(your_json)
print(json.dumps(parsed, indent=4, sort_keys=True))

to call json.loads with your_json to load it into an object.

Then we call json.dumps with the parsed JSON object and indent set to 4 to return a JSON string 4 spaces to indent each level.

Conclusion

To pretty print a JSON file with Python, we can use the json.dumps method with the indent argument.

Categories
Python Answers

How to find the cumulative sum of numbers in a list with Python?

Sometimes, we want to find the cumulative sum of numbers in a list with Python.

In this article, we’ll look at how to find the cumulative sum of numbers in a list with Python.

How to find the cumulative sum of numbers in a list with Python?

To find the cumulative sum of numbers in a list with Python, we can use the numpy cumsum method.

For instance, we write

import numpy as np

a = [4, 6, 12]

s = np.cumsum(a)

to call np.cumsum with list a to return the cumulative sum of the list items as a numpy array.

Conclusion

To find the cumulative sum of numbers in a list with Python, we can use the numpy cumsum method.

Categories
Python Answers

How to parse a date string and change format with Python?

Sometimes, we want to parse a date string and change format with Python.

In this article, we’ll look at how to parse a date string and change format with Python.

How to parse a date string and change format with Python?

To parse a date string and change format with Python, we can call strptime to parse a date string into a datetime object.

And then we call strftime on the datetime object to return a date string in the new format.

For instance, we write

import datetime
d =  datetime.datetime.strptime('Thu Apr 07 2022', '%a %b %d %Y').strftime('%d/%m/%Y')

to call datetime.datetime.strptime with the 'Thu Apr 07 2022' date string and the '%a %b %d %Y' format string to parse it into a datetime object.

Then we call strftime with a new format string to return a date string in the new format.

Conclusion

To parse a date string and change format with Python, we can call strptime to parse a date string into a datetime object.

And then we call strftime on the datetime object to return a date string in the new format.