Categories
Python Answers

How to save an object to disk with Python?

Sometimes, we want to save an object to disk with Python.

In this article, we’ll look at how to save an object to disk with Python.

How to save an object to disk with Python?

To save an object to disk with Python, we can use the pickle module.

For instance, we write:

import pickle


class Company(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value


company = Company('foo', 'bar')


def save_object(obj, filename):
    with open(filename, 'wb') as outp:
        pickle.dump(obj, outp, pickle.HIGHEST_PROTOCOL)


save_object(company, 'company.pkl')

We have the Company class that we instantiated and assigned to company.

Then we define the save_object function that opens filename and call pickle.dump with obj, the outp file and pickle.HIGHEST_PROTOCOL to always save the file with the latest data.

Conclusion

To save an object to disk with Python, we can use the pickle module.

Categories
Python Answers

How to write JSON data to a file with Python?

Sometimes, we want to write JSON data to a file with Python.

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

How to write JSON data to a file with Python?

To write JSON data to a file with Python, we can use the open function and the json.dump method.

For instance, we write:

import json

data = {'foo': 1, 'bar': 2}
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

to define the data dictionary which we want to write into the JSON file.

Then we call open with 'data.json' to open the data.json file.

'w' lets us open the file with write permission.

encoding sets the text encoding of the file.

Then we call json.dump with data and f to write data to file f.

ensure_ascii set to False to skip ASCII check in the file.

indent is set to 2 to indent each level with 2 spaces.

Conclusion

To write JSON data to a file with Python, we can use the open function and the json.dump method.

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 BeautfulSoup.

We install it by running:

pip install beautifulsoup

Then we can write:

from bs4 import BeautifulSoup
html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())

to add an HTML string and parse it with the BeautifulSoup class.

Then we can print the parsed document in the last line.

We can get the links from the HTML string with the find_all method:

from bs4 import BeautifulSoup
html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
for link in soup.find_all('a'):
    print(link.get('href'))

We just pass in the selector for the elements we wan to get.

Also, we can get all the text from the page with get_text():

from bs4 import BeautifulSoup
html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.get_text())

Conclusion

To parse HTML using Python, we can use BeautfulSoup.

Categories
Python Answers

How to pretty printing XML in Python?

Sometimes, we want to pretty print XML in Python.

In this article, we’ll look at how to pretty print XML in Python.

How to pretty printing XML in Python?

To pretty print XML in Python, we can use the xml.dom.minidom.parseString method.

For instance, we write:

import xml.dom.minidom

xml_string = '''
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
'''
dom = xml.dom.minidom.parseString(xml_string)
pretty_xml_as_string = dom.toprettyxml()
print(pretty_xml_as_string)

We call xml.dom.minidom.parseString with xml_string to create the dom object from the XML string.

Then we call dom.toprettyxml to return a prettified version of the XML string.

Therefore, we see:

<?xml version="1.0" ?>
<note>


    <to>Tove</to>


    <from>Jani</from>


    <heading>Reminder</heading>


    <body>Don't forget me this weekend!</body>


</note>

printed.

We can also call xml.dom.minidom.parse with the XML file path and do the same thing.

For instance, we write:

file.xml

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

main.py

import xml.dom.minidom

dom = xml.dom.minidom.parse('file.xml')
pretty_xml_as_string = dom.toprettyxml()
print(pretty_xml_as_string)

and get the same result.

The only difference is that we pass in the file path to xml.dom.minidom.parse.

Conclusion

To pretty print XML in Python, we can use the xml.dom.minidom.parseString method.

We can also call xml.dom.minidom.parse with the XML file path and do the same thing.

Categories
Python Answers

How to create a long multi-line string with Python?

Sometimes, we want to create a long multi-line string with Python.

In this article, we’ll look at how to create a long multi-line string with Python.

How to create a long multi-line string with Python?

To create a long multi-line string with Python, we can enclose our string content with ''' or """.

For instance, we write:

s = """ This is a very
        long string if I had the
        energy to type more and more ..."""

or:

s = ''' This is a very
        long string if I had the
        energy to type more and more ...'''

to define the s multi-line string.

Conclusion

To create a long multi-line string with Python, we can enclose our string content with ''' or """.