[英]AttributeError: '_io.TextIOWrapper' object has no attribute 'reader'
Couldn't get what wrong in the code, as csv
module has a csv.reader()
function as per the documentation . 找不到错误的代码,因为csv
模块根据文档具有csv.reader()
函数。 But I am still getting this error: 但我仍然收到此错误:
Traceback (most recent call last):
File "test_csv.py", line 4, in <module>
read = csv.reader(csv, delimiter = ',')
AttributeError: '_io.TextIOWrapper' object has no attribute 'reader'
My code: 我的代码:
import csv
with open('test_csv.csv') as csv:
read = csv.reader(csv, delimiter = ',')
for row in read:
print(row)
You re-bound the name csv
in the as
target: 您在as
目标中重新绑定了名称csv
:
with open('test_csv.csv') as csv:
This masks the module name, so csv.reader
is resolved on the file object . 这将屏蔽模块名称,因此csv.reader
在文件对象上解析。
Use a different target: 使用其他目标:
with open('test_csv.csv') as csvfile:
read = csv.reader(csvfile, delimiter = ',')
for row in read:
print(row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.