0% found this document useful (0 votes)
9 views10 pages

17 - Working With CSV, JSON, YAML Files

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

IV.

Data writing and transmission


So far, only the simplest option has been covered - writing information to a plain text file.

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.

• YAML format is often used to describe playbooks. For example, it is used in


Ansible. In addition, in this format it is convenient to write manually the parameters that
should be read by scripts.

84
17. Working with CSV, JSON, YAML files
Data serialization is about storing data in some format that is often structured.

For example, it could be:


• files in YAML or JSON format
• files in CSV format
• database

YAML, JSON, CSV formats usage:


• you may have data about IP address and similar information to process in tables
– table can be exported to CSV format and processed by Python
• software can return data in JSON format. Accordingly, by converting this data into
a Python object you can work with it and do whatever you want
• YAML is very convenient to use to describe parameters
– for example, it can be settings for different objects (IP addresses, VLANs,
etc.)
– at least knowing YAML format will be useful when using Ansible

For each of these formats, Python has a module that makes them easier to work with.

Work with CSV files


CSV (comma-separated value) - a tabular data format (for example, it may be data from
a table or data from a database).

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

Example of a CSV file (sw_data.csv):


hostname,vendor,model,location
sw1,Cisco,3750,London
sw2,Cisco,3850,Liverpool
sw3,Cisco,3650,Liverpool

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)

The output is:


['hostname', 'vendor', 'model', 'location']
['sw1', 'Cisco', '3750', 'London']
['sw2', 'Cisco', '3850', 'Liverpool']
['sw3', 'Cisco', '3650', 'Liverpool']

If necessary it could be converted into a list in the following way:


with open('sw_data.csv') as f:
reader = csv.reader(f)
print(list(reader))

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.

For this purpose, module has DictReader:


import csv

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

data = [['hostname', 'vendor', 'model', 'location'],


['sw1', 'Cisco', '3750', 'London, Best str'],
['sw2', 'Cisco', '3850', 'Liverpool, Better str'],
['sw3', 'Cisco', '3650', 'Liverpool, Better str'],
['sw4', 'Cisco', '3650', 'London, Best str']]

with open('sw_data_new.csv', 'w') as f:


writer = csv.writer(f)
for row in data:
writer.writerow(row)

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'
}]

with open('csv_write_dictwriter.csv', 'w') as f:


writer = csv.DictWriter(
f, fieldnames=list(data[0].keys()), quoting=csv.QUOTE_NONNUMERIC)
writer.writeheader()
for d in data:
writer.writerow(d)

Delimiter
Sometimes other values are used as a separator. In this case, it should be possible to tell
module which separator to use.

For example, if the file uses separator ; :


hostname;vendor;model;location
sw1;Cisco;3750;London
sw2;Cisco;3850;Liverpool
sw3;Cisco;3650;Liverpool

Simply specify which separator is used in reader:


import csv

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.

JSON syntax is very similar to Python and is user-friendly.

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"
]
}

There are two methods for reading in json module:


• json.load - method reads JSON file and returns Python objects
• json.loads - method reads string in JSON format and returns Python objects

json.load
Reading JSON file to Python object:
import json

with open('sw_templates.json') as f:
templates = json.load(f)

print(templates)

for section, commands in templates.items():


print(section)
print('\n'.join(commands))

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'
]

to_json = {'trunk': trunk_template, 'access': access_template}

with open('sw_templates.json', 'w') as f:


f.write(json.dumps(to_json))

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}

with open('sw_templates.json', 'w') as f:


json.dump(to_json, f)
with open('sw_templates.json') as f:
print(f.read())

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)

Work with YAML files


YAML (YAML Ain’t Markup Language) - another text format for writing data.

YAML is more human-friendly than JSON, so it is often used to describe actions in


software. Playbooks in Ansible, for example.

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]

Or every item in the list in separate row:


- 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

Work with it is similar to csv and json modules.

Reading from YAML


Converting data from YAML file to Python objects (info.yaml file):
- BS: 1550
IT: 791
id: 11
name: Liverpool
to_id: 1
to_name: LONDON
- BS: 1510
IT: 793
id: 12
name: Bristol
to_id: 1
to_name: LONDON
- BS: 1650
IT: 892
id: 14
name: Coventry
to_id: 2
to_name: Manchester

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'
]

to_yaml = {'trunk': trunk_template, 'access': access_template}

with open('sw_templates.yaml', 'w') as f:


yaml.dump(to_yaml, f, default_flow_style=False)

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

You might also like