2013-10-15 102 views
2

我正在使用以下代碼來學習如何使用本網站的CSV文件進行讀取,寫入和分析http://www.whit.com.au/blog/2011/11/reading-and-writing-csv-files/。但是,我得到這些錯誤,Python CSV模塊

Traceback (most recent call last): Files "csv_example.py", 
line 1, in <module? import csv 
File "C:\python27\csv.py", line 11, in <module> 
mywriter = csv.writer(csv_out) 
AttributeError: 'module' object has no attribute 'writer' 

你知道爲什麼我得到上述錯誤嗎?我正在使用Notepad ++和PowerShell。我實際上四處尋找,並試圖找到一個CSV模塊的Python下載,但我沒有找到任何。我有點迷失。

import csv 

bus_numbers = ['101', '102', '36', '40'] 
bus_names = ['NUC_A', 'NUC_B', 'CATDOG', 'HYDRO_A'] 
voltage = [.99, 1.02, 1.01, 1.00] 

# open a file for writing. 
csv_out = open('mycsv.csv', 'wb') 

# create the csv writer object. 
mywriter = csv.writer(csv_out) 

# all rows at once. 
rows = zip(bus_numbers, bus_names, voltage) 
mywriter.writerows(rows) 

# always make sure that you close the file. 
# otherwise you might find that it is empty. 
csv_out.close() 

在旁註中,我對如何在Python中讀取,寫入和使用CSV文件的教程很感興趣。

+0

官方Python文檔在[csv模塊](http://docs.python.org/2/library/csv.html)上有詳細闡述, –

回答

11

這是一個常見錯誤。將您的腳本從csv.py重命名爲其他內容;它會嘗試導入自己,當你做import csv