我正在使用以下代码来学习如何使用来自该网站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 文件的优秀教程很感兴趣。