如何根据列删除 CSV 文件中的重复行

问题描述 投票:0回答:1
我基本上想删除 CSV 文件中第二列中包含重复单元格的所有行:

Skufnoo,222228888444,-6026769894509215039,ВупÑень пупÑень â¤ï¸â€ðŸ©¹ðŸ’—,AA2888 ចាក់បាល់និងកាសុីណូអនឡាញ (070645555),1746008070,False,False,4,True,False,0 mAtkmb,5213786988,4161254730445748607,ДаниÑль Блинов,AA2888 ចាក់បាល់និងកាសុីណូអនឡាញ (070645555),1746008070,False,False,False,False,False,0 Ethan58,222228888444,7737583697013043644,Ethan,AA2888 ចាក់បាល់និងកាសុីណូអនឡាញ (070645555),1746008070,False,False,4,True,False,0 sheluvjoseph,1421438213,8544915453690665435,អន សំអុល,AA2888 ចាក់បាល់និងកាសុីណូអនឡាញ (070645555),1746008070,False,False,5,True,False,0
并将它们写入新的 CSV 文件,如下所示:

Skufnoo,222228888444,-6026769894509215039,ВупÑень пупÑень â¤ï¸â€ðŸ©¹ðŸ’—,AA2888 ចាក់បាល់និងកាសុីណូអនឡាញ (070645555),1746008070,False,False,4,True,False,0 mAtkmb,5213786988,4161254730445748607,ДаниÑль Блинов,AA2888 ចាក់បាល់និងកាសុីណូអនឡាញ (070645555),1746008070,False,False,False,False,False,0 sheluvjoseph,1421438213,8544915453690665435,អន សំអុល,AA2888 ចាក់បាល់និងកាសុីណូអនឡាញ (070645555),1746008070,False,False,5,True,False,0
我尝试了以下代码,但它不起作用:

import csv with open('members.csv', 'r', encoding="utf8") as in_file, open('members2.csv', 'w', encoding="utf8") as out_file: writer=csv.writer(out_file) tracks = set() for row in in_file: key = row[1] if key not in tracks: writer.writerow(row) tracks.add(key)
非常感谢任何帮助。

python csv duplicates
1个回答
0
投票
您忘记使用 csv.reader 读取输入 csv 文件

in_data = csv.reader(in_file, delimiter=',')
代码中的所有其他行似乎都没问题。

完整代码:

import csv with open('members.csv', 'r', encoding="utf8") as in_file, open('members2.csv', 'w', encoding="utf8") as out_file: in_data = csv.reader(in_file, delimiter=',') writer=csv.writer(out_file) tracks = set() for row in in_data: key = row[1] if key not in tracks: writer.writerow(row) tracks.add(key)
    
© www.soinside.com 2019 - 2024. All rights reserved.