Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A simple csv code
#1
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.
Reply
#2
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".
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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 :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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...
Reply
#6
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))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Quote:strange that it takes only one argument...
There are many examples of the expected syntax format on the web
.
Reply
#8
What is strange is that I found this example in the pretty known book on web scraping.
Reply
#9
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.
Reply
#10
exampleReader = csv.reader(exampleFile, delimiter='\t')
Gary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 318 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 480 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 904 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,441 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,799 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,309 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,602 Sep-06-2021, 12:10 PM
Last Post: jefsummers
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,824 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Plotting sum of data files using simple code Laplace12 3 3,041 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Help with isinstance command (very simple code) Laplace12 2 2,002 Jul-30-2020, 05:26 AM
Last Post: Laplace12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020