AttributeError:“模块"对象没有属性“作家" [英] AttributeError: 'module' object has no attribute 'writer'
问题描述
我正在尝试执行以下程序来读取sublime text2中的csv文件,并收到错误消息"AttributeError:'模块'对象没有属性'writer'" 任何解决方案.
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()
完整错误消息
当前工作目录为F:\ Traceback(最近通话) 最后):readcsv()中的文件"F:\ readfiles.py",第12行,
readcsv readerr = csv.reader(f)中的文件"F:\ readfiles.py",第7行 AttributeError:模块"对象没有属性阅读器" [在1.4秒内完成,退出代码为1]
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]
推荐答案
调试步骤:-
理想情况下,csv
应该具有reader
模块.我最好的猜测是您正在导入其他名为csv
的模块.您可以在python控制台上尝试以下操作:-'
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)
如果找不到reader
,writer
等模块,则可能是您导入了错误的同名模块.现在尝试>>>csv.__file__
,重命名此文件,然后再次执行上一步.
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.
通常,您的代码可以通过以下方式看起来像pythonic:-
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
这篇关于AttributeError:“模块"对象没有属性“作家"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!