从 CSV 列中查找重复项并在写入前删除

Find Duplicates from columns in CSV and Remove before write

我正在通过读取我创建的多个文本文件创建一个 csv 文件,如下所示

Col1,  Col2,  Col3,  Col4
name1, copy, create, copy
       cut           paste

name2, data, null , data
       cut           cut

我想在写入 csv 之前删除 column4column2 中的重复项。像上面的 row1column4 应该只是 paste 就像在 row2[ 中一样=31=], column4 应该是 empty

期望的输出如下:

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 之前删除重复项。任何人都可以建议对此进行任何更新。谢谢

问题不是很清楚。然而,您通常可以做的是将一个列表的元素与另一个列表进行比较和编辑,并从目标列表中删除重复项。假设在这种情况下,col2 是目标列表:

col1 = ['copy','create','cut']
col2 = ['copy','create','cut','delete']

您可以使用列表理解来创建一个只有唯一值的新列表:

col2 = [i for i in col2 if i not in col1 ]

然后如果你打印结果,你会得到 col2:

['delete']