Jinja2 and CSV

CVS can be use as a Dictionary :
1 Line = Keys
2 Line = Values

Each Keys/Values ( Columns ) are separated by a ‘ , ‘
If more then one value is added for one key then it is done by spacing :

To open and read an CSV file :
import jinja2
import csv

var_file = “file.CSV”
with open(var_file) as f:
….var_csv = csv.DictReader(f)

For when there is more then one value :
for dict_vars in var_csv:
…. network_routes = dict_vars[‘network_routes’]
….┬ánetwork_routes =network_routes.split()
….dict_vars[‘network_routes’] =┬ánetwork_routes


Reference :
Python for Network Engineers



Leave a Comment