I am trying to write a csv file with python from a dictionnary which looks like follows:
1: [(3456,12),(4521,78)]
2: [(5478,45),(5609,43)]
3: [(8756,34),(6721,90)]
etc ...
I have several dictionaries like this. They can have until 30 keys. To write all the dictionnaries in a csv file, I wrote the following code which will be applied to each dictionnary:
for i,j in dictionnary.iteritems():
for k in j:
cr=csv.reader(open('/home/file.csv','rb'))
datalist=list(cr)
c=writerows(datalist+[[self.user_id, i,k[0],k[1]])
When I open the csv file, only a part of the dictionary is written. When I run multiple times the code, not the same entries of the dictionary are missing. I don't understand what happens. Where could the problem come from?