Getting an error on one of the sample code I was interested to try:
import csv
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows('h','g')
Attribute error, module has no write attribute.
Help!
Getting an error on one of the sample code I was interested to try:
import csv
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows('h','g')
Attribute error, module has no write attribute.
Help!
I got a different error.
Change writer.writerows('h','g') to writer.writerows(['h','g']).
If you are trying to run in Python3 then change the file open() mode from wb to just w.
Error persists
@lvinci , almost the same as @ccc , but rather than a list a dict.
Like - the below. It does talk about a dict in the help.
import csv
with open('some.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows({'h','g'})
>>> 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 for csv.writer with an r 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.