Appending a dictionary to csv file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Appending a dictionary to csv file (/thread-25859.html) |
Appending a dictionary to csv file - zahra_vaziri - Apr-14-2020 Hi, In a for loop I initially have the following dictionary: dict1={'a':[[1,2],[3,4],[5,6]],'b':[[11,22],[33,44],[55,66]],'c':[[111,222],[333,444],[555,666]]} I want to write it to a csv file as such: In the next iteration of the loop dict1 becomes:dict1={'a':[[7,8],[9,10]],'b':[[77,88],[99,100]],'c':[[777,888],[999,1000]] and I want to append this to the previous csv file as such: I currently have this code:if len(dict1)!=0: with open('dict1.csv', 'a+', encoding='utf8', newline='') as f: fc = csv.DictWriter(f, fieldnames=dict1.keys()) fc.writerows(dict1)This result in the following error: Any ideas how I might do this correctly?
|