Hi,
In a for loop I initially have the following dictionary:
I want to write it to a csv file as such:
and I want to append this to the previous csv file as such:
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:
Output:[1,2],[3,4],[5,6]
[11,22],[33,44],[55,66]
[111,222],[333,444],[555,666]
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:
Output:[1,2],[3,4],[5,6],[7,8],[9,10]
[11,22],[33,44],[55,66],[77,88],[99,100]
[111,222],[333,444],[555,666],[777,888],[999,1000]
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:
Error:Traceback (most recent call last):
File "Gain_readDataT.py", line 279, in signalGain
fc.writerows(dict1)
File "/usr/lib/python3.6/csv.py", line 158, in writerows
return self.writer.writerows(map(self._dict_to_list, rowdicts))
File "/usr/lib/python3.6/csv.py", line 148, in _dict_to_list
wrong_fields = rowdict.keys() - self.fieldnames
AttributeError: 'str' object has no attribute 'keys'
Any ideas how I might do this correctly?