我正在使用以下代码来了解如何使用此网站http://www.whit.com.au/blog/2011/11/reading-and-writing-csv-files/中的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 :(得分:11)
这是一个常见的错误。将脚本从csv.py重命名为其他内容;当你import csv
时,它会尝试导入自己。