无法在代码中出错,因为csv
模块根据the documentation具有csv.reader()
函数。但我仍然收到这个错误:
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'
我的代码:
import csv
with open('test_csv.csv') as csv:
read = csv.reader(csv, delimiter = ',')
for row in read:
print(row)
答案 0 :(得分:2)
您重新绑定了csv
目标中的名称as
:
with open('test_csv.csv') as csv:
这会掩盖模块名称,因此csv.reader
已在文件对象上解析 。
使用其他目标:
with open('test_csv.csv') as csvfile:
read = csv.reader(csvfile, delimiter = ',')
for row in read:
print(row)