AttributeError:' _io.TextIOWrapper'对象没有属性'读者'

时间:2016-07-22 09:49:19

标签: python csv

无法在代码中出错,因为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)

1 个答案:

答案 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)