Saturday, 10 August 2013

Write to csv: columns are shifted when item in row is empty (Python)

Write to csv: columns are shifted when item in row is empty (Python)

Basic read, al is ok:
with open('kres.csv', 'w', newline='') as f: # Just use 'w' mode in 3.x
writer = csv.writer(f)
writer.writerows(kres)
here i am writing to csv, but columns are shifted when field (item) in row
is empty, that's (i assume) because program doesn't know how many columns
in file and writes it one by one.
But i want it not to skip empty field, i want it to write some default
character or None. I don't know how to check field is empty.
with open('mycsv.csv', 'w') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, my_dict.keys())
w.writeheader()
w.writerow(my_dict)
Python 3.3.2 on Windows 7

No comments:

Post a Comment