【发布时间】:2020-09-10 05:26:35
【问题描述】:
我正在通过读取我创建的多个文本文件来创建一个 csv 文件,如下所示
Col1, Col2, Col3, Col4
name1, copy, create, copy
cut paste
name2, data, null , data
cut cut
我想在写入 csv 之前从 column4 中删除与 column2 的重复项。像上面的 row1 一样,column4 只能像 row2、column4 一样粘贴 > 应该是空
想要的输出是这样的:
Col1, Col2, Col3, Col4
name1, copy, create, paste
cut
name2, data, null ,
cut
我有类似下面的东西
stat2 = 'Col1,Col2,Col3,Col4\n'
text_file=os.listdir('.data/')
for pack in text_file:
file = open("./data/"+ pack, "r")
perp = file.read()
stat2 += pack + ',"'
#I'm iterating through different set of list and matching with all multiple files.
for word in package:
stat2 += word + "\n"
stat2 += '","'
for word in data:
stat2 += word + "\n"
stat2 += '","'
for word in file:
stat2 += word + "\n"
stat2 += '"' + "\n"
f = open("data/csv_file.csv", "w")
f.write(stat2)
我想在将其写入 csv 之前删除重复项。任何人都可以建议对此进行任何更新。谢谢
【问题讨论】:
-
您能否阐明所需输出中的换行符发生了什么。第 2 列中的第二个值真的在单独的一行吗?
-
我只是比较带有列表的文本文件并添加到列中。
-
你能代表文本文件中出现的输入吗?
标签: python python-3.x csv duplicates export-to-csv