2016-07-22 37 views
0

無法得到代碼中的錯誤,因爲csv模塊的csv.reader()函數按照the documentation。但我仍然得到這個錯誤:AttributeError:'_io.TextIourapper'對象沒有屬性'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

as目標您重新綁定的名稱csv

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) 
+0

感謝馬亭,轉產到 「打開( 'test_csv.csv')作爲csvfile」 工作過。它導致了衝突,從而導致了錯誤。 –

相關問題