Dictionary to a CSV file is probably a good question, if you are hiring a VBA developer. For a python person it should be a trivial one, as this one should not be even a tough search in Google. Anyway, there could be a few hints, that could be needed.
Anyway, it could happen, that if you do not explicitly specify the newline named parameter in writing the csv file, you will get an empty line. And you have to debug and search again. That’s why I am putting the whole code here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import csv my_dict = [ {"team": "CSKA", "city": "Sofia", "year": "1948"}, {"team": "Lokomotiv", "city": "Plovdiv", "year": "1924"}, {"team": "Slavia", "city": "Sofia", "year": "1913"}, ] fields = ["team", "year", "city"] filename = "foo_bar.csv" with open(filename, "w", newline="") as csv_file: writer = csv.DictWriter(csv_file, fieldnames=fields) writer.writeheader() writer.writerows(my_dict) |
The result looks as fancy as the one below. As you see, because the second item in fields is “year”, it is listed second in the csv. Small tricks, but interesting:
That’s all!