我已經嘗試過這種方法,但它只會在整行相同的情況下找到重復項第 2 列,數字不在檔案中,只是 csv 的
996,0
996,1.67
123,0
123,8.13
456,0
456,0.00001
seen_rows =[] duplicate_rows =[] for row in csv.reader(in_file): if row in seen_rows: duplicate_rows.append(row) print("Duplicate entry found in entry.txt file please correct this issue then run the program again. duplicates are as follows:", duplicate_rows) else: seen_rows.append(row) print(seen_rows)
如果有人能指出我正確的方向,那就太好了,請隨時解釋解決方案是如何作業的,我對 python 還是很陌生,我在此先感謝。
uj5u.com熱心網友回復:
我為那些想知道的人找到了一種如下所示的方法
with open("filename.txt or .csv", "r") as in_file: # opens the file you want to check
seen_rows =[] # empty list to store seen rows in
duplicate_rows =[] # empty list to store duplicate files in
for row in in_file: # for every row in the file do the below
columns = row.strip().split(",") # define columns in row using strip reomve spaces and split columns by csv 0,1,2,3,4,5,6 etc...
if columns[0] in seen_rows: # if column 1 value is already in seen rows then add the duplicate value inot the duplicate list
duplicate_rows.append(columns[0])
else:
seen_rows.append(columns[0]) # if value not in seen list add it to seen list
if not duplicate_rows:
##do whatever you want to do as there are no duplicate files,(if not duplicate_rows = false) as list is empty
else:
print("Duplicate number found in column 1") ##because the list is not empty (if not duplicate = True) forcing the code to use the else statement
如果有人有另一種方法,請隨時讓我知道更好的方法。
PS如果我做了任何不正確的假設,這種方法在沒有熊貓的情況下也可以作業,請更正代碼謝謝希望這可以幫助那里的人理解它是如何作業的
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465551.html
