2

I am trying to execute this following program to read csv file in sublime text2 gets error message "AttributeError: 'module' object has no attribute 'writer'" Any Solution.

import sys
import csv
def readcsv():
  f = open("F://xyz.csv",'r')
  readerr=csv.reader(f)
  for row in readerr():
      print row
  f.close()
readcsv()

FULL ERROR MESSAGE

The current working directory is F:\ Traceback (most recent call last): File "F:\readfiles.py", line 12, in readcsv()
File "F:\readfiles.py", line 7, in readcsv readerr=csv.reader(f) AttributeError: 'module' object has no attribute 'reader' [Finished in 1.4s with exit code 1]

5
  • So does this file actually exist? is f actually a valid file object? Commented Apr 17, 2015 at 10:58
  • Sorry the error is :AttributeError: 'module' object has no attribute 'reader' Commented Apr 17, 2015 at 10:58
  • You obviously have another module named csv in your sys.path - to know which one, just add a print csv line just after the import csv one. Commented Apr 17, 2015 at 11:00
  • 2
    Well aside from your error this line looks iffy: for row in readerr(): shouldn't it be for row in readerr:? Commented Apr 17, 2015 at 11:00
  • @EdChum .. True but the error is on the line before that. Commented Apr 17, 2015 at 11:15

4 Answers 4

4

Debugging steps:-

Ideally csv should have reader module. My best guess is you have some other module named csv which is being imported. Can you try the following on python console:-'

>>>import csv
>>>dir(csv)

If you do not find the reader, writer etc. modules, chances are you are importing a wrong module with same name. Now try >>>csv.__file__, Rename this file and follow previous step once again.

In general your code could look pythonic the following way:-

with open('csvfile.csv', 'rb') as csvfile:
     rows = csv.reader(csvfile)
     for row in rows:
         print row
Sign up to request clarification or add additional context in comments.

Comments

0

The syntax is slightly off.

for row in readerr():

The parenthesis is function syntax. The command should only be acting on an object.

If you change that line to this, it will work for row in reader:

1 Comment

OOPS , the revised syntax did not make it into the answer for row in readerr:
0

you must have named the file as csv.py, renaming it would troubleshoot the error.

Comments

0

I had this same error and the reason was my file name was also called 'csv.py' Try changing it to something else and try again. Should work !! :)

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.