Why do I keep getting the error "AttributeError: '_io.TextIOWrapper' object has no attribute 'writerows" when using csv module?

Why do I keep getting the error "AttributeError: '_io.TextIOWrapper' object has no attribute 'writerows" when using csv module?

我导入的是 CSV,文档告诉我的方法导致失败。

这是有问题的代码。

outfile = open("D:/stock_information/processed data/standard_deviant.csv", "w")
write_outfile = csv.writer(outfile)

for i in range(len(all_standard_deviant_info)):
    outfile.writerows(all_standard_deviant_info[i][0])
    outfile.writerows(all_standard_deviant_info[i][1])

有谁知道为什么说 .writerows 不存在?

你需要使用,

write_outfile.writerows(all_standard_deviant_info[i][0])

因为 outfile 只是一个文件对象而不是 csv.writer 的对象。