17 - Working With CSV, JSON, YAML Files
17 - Working With CSV, JSON, YAML Files
17 - Working With CSV, JSON, YAML Files
This section covers data reading and writing in CSV, JSON and YAML formats:
• CSV - a tabular format of data presentation. It can be obtained, for example, by
exporting data from a table or database. Similarly, data can be written in this format for
further import into the table.
• JSON - a format that is often used in API. In addition, this format will allow you to
save data structures such as dictionaries or lists in a structured format and then read them
from a JSON file and get the same data structures in Python.
84
17. Working with CSV, JSON, YAML files
Data serialization is about storing data in some format that is often structured.
For each of these formats, Python has a module that makes them easier to work with.
In this format, each line of a file is a line of a table. Despite the format name the separator
can be not only a comma. Formats with a different separator may have their own name, for
example, TSV (tab separated values), however, the name CSV usually means any
separators).
The standard Python library has a csv module that allows working with files in CSV format.
85
Reading
Example of reading a file in CSV format:
import csv
with open('sw_data.csv') as f:
reader = csv.reader(f)
for row in reader:
print(row)
Most often column headers are more convenient to get by a separate object:
import csv
with open('sw_data.csv') as f:
reader = csv.reader(f)
headers = next(reader)
print('Headers: ', headers)
for row in reader:
print(row)
Sometimes it is more convenient to get dictionaries in which keys are column names and
values are column values.
with open('sw_data.csv') as f:
reader = csv.DictReader(f)
for row in reader:
print(row)
print(row['hostname'], row['model'])
86
The output is:
{'hostname': 'sw1', 'vendor': 'Cisco', 'model': '3750',
'location': 'London, Globe Str 1'}
sw1 3750
{'hostname': 'sw2', 'vendor': 'Cisco', 'model': '3850',
'location': 'Liverpool'}
sw2 3850
{'hostname': 'sw3', 'vendor': 'Cisco', 'model': '3650',
'location': 'Liverpool'}
sw3 3650
Writing
Similarly, a csv module can be used to write data to file in CSV format:
import csv
with open('sw_data_new.csv') as f:
print(f.read())
To write strings in a CSV file with quotes you should add change script this way:
writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
DictWriter
With DictWriter you can write dictionaries in CSV format.
In general, DictWriter works as writer but since dictionaries are not ordered it is
necessary to specify the order of columns in file. The fieldnames option is used for this
purpose:
87
import csv
data = [{
'hostname': 'sw1',
'location': 'London',
'model': '3750',
'vendor': 'Cisco'
}, {
'hostname': 'sw2',
'location': 'Liverpool',
'model': '3850',
'vendor': 'Cisco'
}]
Delimiter
Sometimes other values are used as a separator. In this case, it should be possible to tell
module which separator to use.
with open('sw_data2.csv') as f:
reader = csv.reader(f, delimiter=';')
for row in reader:
print(row)
88
Work with JSON files
JSON (JavaScript Object Notation) - a text format for data storage and exchange.
As for CSV, Python has a module that allows easy writing and reading of data in JSON
format.
Reading
File sw_templates.json:
{
"access": [
"switchport mode access",
"switchport access vlan",
"switchport nonegotiate",
"spanning-tree portfast",
"spanning-tree bpduguard enable"
],
"trunk": [
"switchport trunk encapsulation dot1q",
"switchport mode trunk",
"switchport trunk native vlan 999",
"switchport trunk allowed vlan"
]
}
json.load
Reading JSON file to Python object:
import json
with open('sw_templates.json') as f:
templates = json.load(f)
print(templates)
89
Writing
Writing a file in JSON format is also fairly easy.
There are also two methods for writing information in JSON format in json module:
• json.dump - method writes Python object to file in JSON format
• json.dumps - method returns string in JSON format
json.dumps()
Convert object to string in JSON format:
import json
trunk_template = [
'switchport trunk encapsulation dot1q', 'switchport mode trunk',
'switchport trunk native vlan 999', 'switchport trunk allowed vlan'
]
access_template = [
'switchport mode access', 'switchport access vlan',
'switchport nonegotiate', 'spanning-tree portfast'
]
with open('sw_templates.json') as f:
print(f.read())
Method json.dumps is suitable for situations where you want to return a string in JSON
format. For example, to pass it to the API.
json.dump
Write a Python object to a JSON file:
import json
trunk_template = [
'switchport trunk encapsulation dot1q', 'switchport mode trunk',
'switchport trunk native vlan 999', 'switchport trunk allowed vlan'
]
access_template = [
'switchport mode access', 'switchport access vlan',
'switchport nonegotiate', 'spanning-tree portfast'
]
to_json = {'trunk': trunk_template, 'access': access_template}
90
Additional parameters of write methods
By passing additional parameters to dump method (or dumps method) you can get a more
readable output:
json.dump(to_json, f, sort_keys=True, indent=2)
YAML syntax
Like Python, YAML uses indents to specify the structure of document. But YAML can only
use spaces and cannot use tabs. Another similarity with Python is that comments start with
# and continue until the end of line
List
A list can be written in one line:
[switchport mode access, switchport access vlan, switchport
nonegotiate, spanning-tree portfast, spanning-tree bpduguard
enable]
When a list is written in such a block, each row must start with ''- '' (minus and space) and
all lines in the list must be at the same indentation level.
Dictionary
A dictionary with two keys: access and trunk. Values that correspond to these keys -
command lists:
91
access:
- switchport mode access
- switchport access vlan
- switchport nonegotiate
- spanning-tree portfast
- spanning-tree bpduguard enable
trunk:
- switchport trunk encapsulation dot1q
- switchport mode trunk
- switchport trunk native vlan 999
- switchport trunk allowed vlan
PyYAML module
Python uses a PyYAML module to work with YAML. It is not part of the standard module
library, so it needs to be installed:
pip install pyyaml
92
Reading from YAML:
import yaml
from pprint import pprint
with open('info.yaml') as f:
templates = yaml.safe_load(f)
pprint(templates)
YAML format is very convenient for storing different parameters, especially if they are filled
manually.
Writing to YAML
Write Python objects to YAML:
import yaml
trunk_template = [
'switchport trunk encapsulation dot1q', 'switchport mode trunk',
'switchport trunk native vlan 999', 'switchport trunk allowed vlan'
]
access_template = [
'switchport mode access', 'switchport access vlan',
'switchport nonegotiate', 'spanning-tree portfast'
]
with open('sw_templates.yaml') as f:
print(f.read())
File sw_templates.yaml:
access:
- switchport mode access
- switchport access vlan
- switchport nonegotiate
- spanning-tree portfast
- spanning-tree bpduguard enable
trunk:
- switchport trunk encapsulation dot1q
- switchport mode trunk
- switchport trunk native vlan 999
- switchport trunk allowed vlan
93