我正在尝试从 cvs 文件中读取并将输出写入 txt 文档:
import csv
reader = csv.reader(open('C:\Python33\excel.csv', newline=''), delimiter=' ', quotechar='|')
text_file = open("C:\Python33\output.txt", "w")
c = ", "
for row in reader:
text_file.write(', '.join(row))
text_file.write(c)
#reader.close()
text_file.close()
当前输出如下所示:
current: `beef, chicken, corn, cheese, yogurt, hippo,`
desired: `beef, chicken, corn, cheese, yogurt, hippo`
我想删除列表末尾的逗号,我正在考虑一个 if 语句来检查我是否在列表的末尾?但我不太确定该怎么做。