12

please tell me what's the problem in this code it's giving an error

import csv
with open('some.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row
8
  • what is the error ur getting? Commented Apr 26, 2011 at 9:40
  • 1
    The code looks fine to me. Without the error, or the contents of 'some.csv' it is difficult to help. Commented Apr 26, 2011 at 9:43
  • with open('some.csv', 'rb') as f showing syntax error as invalid syntax Commented Apr 26, 2011 at 9:44
  • the error given is some basic information you should give if you want to get an answer for a question like this. Commented Apr 26, 2011 at 9:48
  • 11
    sheesh, guys, some patience for a new user. If you don't have anything constructive to say, leave your jokes for the pub Commented Apr 26, 2011 at 9:52

2 Answers 2

18

Which version of Python are you using?

The with statement is new in 2.6 - if you're using 2.5 you need from __future__ import with_statement. If you use a Python older than 2.5 then there's no with statement, so just write:

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

It's really better to update to a modern version of Python, though. Python 2.5 was released almost 5 years ago, and the current version in the 2.x line is 2.7

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

1 Comment

@atul: Please do not bury important information in a comment. Please update the question to contain all the facts.
6
from __future__ import with_statement

And if that doesn't work, rewrite it to not use with in the first place.

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.