Ask Your Question

Revision history [back]

For such a simple example (only one column), you can avoid the use of the csv module, and add the strings directly.

sage: vect = ['00011', '00010', '01011', '01111', '01100']
sage: with open('file_path_vec.csv', 'w') as f:
....:     f.write('\n'.join(vect))

For The issue is that c.writerows waits for an iterable of iterables, meaning that each element of the list C should be an iterable of strings. Since the elements of C are a single string, and not a list of strings, the methods iterates over the string, compare:

sage: s = 'aze'
sage: for i in s:
....:     print i
a
z
e

with

sage: s = ['aze']
sage: for i in s:
....:     print i
aze

So, if you want to use the csv module you have to give a list of lists of strings:

sage: C = [[i] for i in vect]
sage: C
[['00011'], ['00010'], ['01011'], ['01111'], ['01100']]
sage: with open('file_path_vec.csv', 'w') as f:
....:       c = csv.writer(f)
....:       c.writerows(C)

That said, for such a simple example (only one column), you can avoid the use of the csv module, and add the strings directly.

sage: vect = ['00011', '00010', '01011', '01111', '01100']
sage: with open('file_path_vec.csv', 'w') as f:
....:     f.write('\n'.join(vect))