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]
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
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:
您必须将文件命名为 csv.py,重命名它可以解决错误。
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 !! :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.