import csv
csvFile = open("c:\Python36\files\test.csv", 'w+')
try:
writer = csv.writer(csvFile)
writer.writerow(('number', 'number plus 2', 'number times 2'))
for i in range(10):
writer.writerow(i, i+2, i*2)
finally:
csvFile.close()
Error:
Traceback (most recent call last):
File "C:\Python36\kodovi\csvv.py", line 9, in <module>
csvFile = open("c:\Python36\files\test.csv", 'w+')
OSError: [Errno 22] Invalid argument: 'c:\\Python36\x0ciles\test.csv'
What did I do wrong? Tried several combinations with several paths but it gave me similar error. I didn't create files directory, expecting that it will be created as per this code.
The backslash is an escape character. So you need to either:
1) escape it: "c:\\Python36\\files\\test.csv"
2) use a raw string: r"c:\Python36\files\test.csv"
3) use forward slashes: "c:/Python36/files/test.csv"
You can tell this from the error message, where instead of "files", it's looking for a folder called "Python36\x0ciles".
Backslash is an escape character in Python. You can do three things: use forward slashes instead (they'll work on any system), use two backslashes everytime you want one, or make it a raw string that ignores escape characters by putting an r in front it.
Don't use backslash as separator, It's an escape char, so '\t' is actually Tab.
make
"c:\Python36\files\test.csv"
raw string -
r"c:\Python36\files\test.csv"
use forward slash
"c:/Python36/files?test.csv"
or escape the backslash
"c:\\Python36\\files\\test.csv"
I think this deserves a thread in
Common pitfalls and what to do :-)
Thank you, I opted for a raw string and now get this error:
Error:
Traceback (most recent call last):
File "C:\Python36\kodovi\csvv.py", line 14, in <module>
writer.writerow(i, i+2, i*2)
TypeError: writerow() takes exactly one argument (3 given)
strange that it takes only one argument...
you must supply list, tuple, etc.
writer.writerow((i, i+2, i*2))
as an alternative to
for i in range(10):
writer.writerow((i, i+2, i*2))
you can do
writer.writerows((i, i+2, i*2) for i in range(10))
Some more advise - use
with
context manager to open the file (it will close it for you at the end automatically). You don't need the try/finally
import csv
with open(r"c:\Python36\files\test.csv", 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(('number', 'number plus 2', 'number times 2'))
writer.writerows((i, i+2, i*2) for i in range(10))
Quote:strange that it takes only one argument...
There are many examples of the expected syntax format on the web
.
What is strange is that I found this example in the pretty known book on web scraping.
practising csv I wrote this code:
with open('example.csv', 'r') as exampleFile:
exampleReader = csv.reader(exampleFile)
for line in exampleReader:
print('Line #' + str(exampleReader.line_num) + ' ' + str(line))
this is what I get:
Output:
Line #1 ['4/5/2015 13:34\tApples\t 73']
Line #2 ['4/5/2015 3:41\tCherries 85\t']
Line #3 ['4/6/2015 12:46\tPears\t 14']
Line #4 ['4/8/2015 8:59\tOranges\t 52']
Line #5 ['4/10/2015 2:07\tApples\t 152']
Line #6 ['4/10/2015 18:10\tBananas\t 23']
Line #7 ['4/10/2015 2:40\tStrawberries\t98']
Do you know what to do to eliminate this '\t'? I don't understand why this tab sign appears at all since I didn't add it.
exampleReader = csv.reader(exampleFile, delimiter='\t')