with open("output.csv", 'wb') as g:
writer = csv.writer(g)
for item in new_text_list:
writer.writerow([item]) # writerow (singular), not writerows (plural)
or
with open("output.csv", 'wb') as g:
writer = csv.writer(g)
writer.writerows([[item] for item in new_text_list])
When you use writerows
, the argument should be an iterator of rows, where each row is a iterator of field values. Here, the field value is item
. So a row could be the list, [item]
. Thus, writerows
can take a list of lists as its argument.
writer.writerows([val])
did not work because [val]
is just a list containing a string, not a list of lists.
Now strings are also sequences -- a sequence of characters:
In [164]: list('abc')
Out[164]: ['a', 'b', 'c']
So writerows
took [val]
to be a list containing a row
, val
. Each character represented a field value. So the characters in your string got splattered. For example,
import csv
with open('/tmp/out', 'wb') as f:
writer = csv.writer(f)
writer.writerows(['Hi, there'])
yields
H,i,",", ,t,h,e,r,e