module'csv'没有属性'writerow'

时间:2018-06-15 17:12:24

标签: python-3.x

尝试使用以下命令将列表写入csv文件:

import csv

with open('filename.csv', 'w', newline='') as csvfile:
    wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
    wr = csv.writerow(list)

返回错误:

module 'csv' has no attribute 'writerow'

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您必须在writerow() - 实例上致电writer,而不是csv

来自the documentation的Straigth:

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])