我需要在第 8 列之后的每条记录中查找重复值,如果有,请将其删除。每条记录的前两个值可以相同,不能删除。
with open("User.csv") as file:
reader = csv.reader(file)
for row in reader:
for column in reader:
print(column)
上面的代码读取控制台中的文件:
['257488', '257488', '3', '1234', '', '1', '1', '', '160000', '', '0', '', '']
['257488', '257488', '3', '1234', '', '1', '1', '', '160000', '', '0', '', '']
['270076', '270076', '2', '1234', '', '1', '1', '', '40000', '270076CASH', '270076CASH', '', '']
['270076', '270076', '2', '1234', '', '1', '1', '', '40000', '270076CASH', '0', '', '']
注意:上面第 3 行中的第二个值“270076CASH”要删除。它应该检查并删除其余行。
最佳答案
解决此问题的一个解决方案是获取第 8 列之后的所有项目,并仅存储唯一的项目(通过标准 for
循环和 if
语句进行检查)。然后,取出第 8 列后的这些唯一项目并将它们插入回记录中。下面是一个相当多评论的实现:
with open( 'User.csv' ) as file:
reader = csv.reader( file )
for record in reader:
# store all items after column 8 into a temporary list
subset_list = record[9:]
# keep track of unique (non-duplicate) items in the subset
unique_subset = []
# loop through every item of the subset
for item in subset_list:
# check if the current item already exists, then append
if not item in unique_subset or item == '':
unique_subset.append( item )
# after looping through subset, reinsert the unique entries into column
new_record = record[:9] + unique_subset[:]
# verify operations
print( new_record )
关于python - 删除 CSV 中某些列中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56172943/