when i'm saving a dictionary representing a graph with the format:
graph_dict = {'b': ['a', 'c'], 'a': [], 'c': ['a'], 'd': []}
and save it with the csv.DictWriter and load it i get:
loaded_graph ={'b': "['a', 'c']", 'c': "['a']", 'a': '[]', 'd': '[]'}
How can i avoid the added quotation marks for the value lists or what code do i have to use to remove them when reading the file? Help would be appreciated!
print(graph_dict)
with open('graph.csv', 'w') as csvfile:
graph = ['vertices', 'edges']
writer = csv.DictWriter(csvfile, fieldnames=graph)
writer.writeheader()
for vertex in graph_dict:
edges = graph_dict[vertex]
writer.writerow({'vertices': vertex, 'edges': edges})
print("reading")
loaded_graph = {}
with open('graph.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
loaded_graph[row['vertices']] = row['edges']
print(loaded_graph)
the csv file opened in editor looks like this:
vertices,edges
b,"['a', 'c']"
a,[]
c,['a']
d,[]