Python 3 latest code crumbs

Read YAML file with multiple documents with Python

Read YAML file with multiple documents with Python

Dec 10, 2021 by erik

PyYAML can read YAML files that contain multiple documents by using the yaml.safe_load_all() function. Note that the file needs to be opened as long as you're reading documents from the YAML, so you have to do your processing within the...

yaml files
How to read YAML with Python and PyYAML

How to read YAML with Python and PyYAML

Dec 10, 2021 by erik

There are multiple Python packages that can parse YAML data. However, PyYAML is the most prevalent and also the most complete implementation for parsing YAML. PyYAML is not part of the standard Python library, meaning you need to install it...

yaml files
Using docstrings to document your code

Using docstrings to document your code

Dec 8, 2021 by erik

A docstring is a string that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object. You can read docstrings with the help() function....

docstring
Encoding JSON with json.dumps

Encoding JSON with json.dumps

Dec 7, 2021 by erik

Encoding JSON data with Python's json.dumps is just as easy as decoding. Use json.dumps (short for ‘dump to string') to convert a Python object consisting of dictionaries, lists, and other native types into a string. To learn more about JSON...

json encoding
Read JSON with json.loads

Read JSON with json.loads

Dec 7, 2021 by erik

Parsing a string of JSON data, also called decoding JSON, is as simple as using json.loads(…). Loads is short for load string. It converts: - objects to dictionaries - arrays to lists, - booleans, integers, floats, and strings are recognized...

decoding json
Python append to text file

Python append to text file

Dec 3, 2021 by erik

This is how you append data to a text file. 1. First, we open the file and write a couple of lines using the write file mode 'w'. 2. Next, we open it again to append some more lines using...

range files append