Python CSV模块

Python CSV模块,python,csv,Python,Csv,我正在使用以下代码学习如何使用本网站的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' o

我正在使用以下代码学习如何使用本网站的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'

另一方面,我对如何在python中读取、写入和使用CSV文件的良好教程很感兴趣。

这是一个常见错误。将脚本从csv.py重命名为其他内容;当您执行导入csv时,它将尝试导入自身。Python官方文档中有一节详细介绍了csv
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()