Reputation: 2002
This is my set:
set(['description', 'title'])
I need this to write to a csv file in 2 columns. My code:
cw = csv.writer(open("hello.csv",'w'))
cw.writerows(cols)
Throws an error:
cw.writerow(cols)
_csv.Error: sequence expected
Add list to csv file:
cw.writerow(list(cols))
for row in data:
cw.writerow([str(row.get(k,'N/A')) for k in cols])
Found a way to rectify this: Open a file in wb mode rather than in w mode
Upvotes: 6
Views: 31389
Reputation: 1123710
Turn your set into a list first:
cw.writerow(list(cols))
Note that the order in which the columns are written will be arbitrary; sets, like dictionaries, have no fixed order.
Upvotes: 8