【发布时间】:2019-02-28 13:19:25
【问题描述】:
需要帮助!使用以前的主题,我发现了如何从 csv 文件中读取数据,对此我没有问题,但我无法将特定列(例如 file.csv 中的第 4 列)保存为 new.csv 文件。我的脚本正确打印了第 4 列,但没有保存它。
import csv
with open('file.csv') as csvfile:
file1 = csv.reader(csvfile, delimiter=',')
fourth_col = []
for cols in file1:
fourth_col = cols[3]
print (fourth_col)
new_file = open('new.csv', 'w')
writer = csv.writer(new_file)
writer.writerows(fourth_col)
new_file.close()
【问题讨论】: