0

How to read a csv file script Python?

import csv
with open('some.csv', 'rb') as f:
  reader = csv.reader(f)
  for row in reader:
     print row

gives this error

python2 test.py 
Traceback (most recent call last):
File "test.py", line 1, in <module>
import csv
File "/home/miruss/virtualbox/csv.py", line 4, in <module>
  f = open(sys.argv[1], 'rt')
IndexError: list index out of range

the reason for the error do not understand.

2
  • 1
    Emm... error doesn't match your script Commented Dec 1, 2014 at 23:13
  • 1
    You might want to edit your post with the code fragments that are necessary to reproduce the problem. Commented Dec 1, 2014 at 23:15

2 Answers 2

2

Your error comes from "sys.argv[1]".

You meant to execute your code against the file name.

python2 test.py filename.csv

Sign up to request clarification or add additional context in comments.

2 Comments

good job for actually seeing that his question really had nothing to do with csv ...
python2 test.py some.csv Traceback (most recent call last): File "test.py", line 1, in <module> import csv File "/home/miruss/virtualbox/csv.py", line 6, in <module> reader = csv.reader(f) AttributeError: 'module' object has no attribute 'reader'
0
print list(csv.reader(open("somefile.csv")))

would work I think

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.