你如何在python程序中写入.csv文件?

时间:2017-07-11 14:28:15

标签: python-3.x

我这样做了但是我收到了一个错误 - '作家没有被定义'。

import csv

x = {1,2,3}
file = open('xyz.csv', 'w')
write = csv.writer(file)
for i in x:
    writer.writerow((i,3))
file.close()

1 个答案:

答案 0 :(得分:0)

您从未定义过writer。您说write = csv.writer(file),请考虑将其更改为writer = csv.writer(file)

您的代码将是:

import csv

x = {1,2,3}
file = open('xyz.csv', 'w')
writer = csv.writer(file)
for i in x:
    writer.writerow((i,3))
file.close()