Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Attribute error on csv write
-
I got a different error.
Change
writer.writerows('h','g')
towriter.writerows(['h','g'])
.If you are trying to run in Python3 then change the file open() mode from
wb
to justw
. -
Error persists
-
-
>>> print(type({'h','g'})) <class 'set'>
-
@lvinci The error above says that the failure is occurs when looking for an module-level attribute called
write
but your code is looking forcsv.writer
with anr
at the end. Are you sure that you are trying to run only the four lines above and that there is no typo (write vs. writer)?Can you tell us the version of Pythonista that you are running? Open the file list at the left of the Pythonista editor. At the bottom, click on the gear icon to open Settings. Scroll all the way to the bottom of Settings to see something like:
v2.1.1
. -
Yes, typo. I meant writer obviously.
Vs 2.1 (210000)
-
Also on vs. 3.0
Can anyone give and example on how to write a CSV file, please? Thank you. -
@lvinci , what's your data? A cvs file normally is just a series of double quoted items separated by the delimiter comma, with a terminating line sequence depending on the os. There is really nothing so special about cvs files. If you change the delimiter to a tab then they call it a tab delimited file. Just jargon
-
Sorry, CSV. But really it's just a list of coma delimited items (fields) that constitute a record (lines) with some attention to make the items (fields) in each record are quoted correctly. Eg. "Don't"
-
Write a list of lists to a CSV file...
import csv chars = 'abcdefghij' matrix = [[x for x in chars.lower()], [i for i in range(10)], [x for x in chars.upper()], [i + .5 for i in range(10)]] with open('testfile.csv', 'w', newline='') as out_file: csv.writer(out_file).writerows(matrix) # now read in the file and print out its contents with open('testfile.csv') as in_file: print(''.join(in_file))
Notice that the output contains no double quoted items.