我有两个CSV文件,相同的字段,让我们说:
ID,NAME,SURNAME,HOME_ADDRESS,NUMBER_OF_PHONE_LINES,PREFIX,PHONE_NUMBER,EMAIL
我想比较两个CSV文件,然后找到:
最后,将这些信息分成三个不同的文件。
有人知道如何实现这个目标吗?
答案 0 :(得分:0)
我会尝试提出一个整体想法。这没有经过测试,只是为了描述这个想法,而不是解决方案!
# open files...
csv_a = f1.readlines()
box_a = [x.split(',') for x in csv_a]
#similar to load list of lists for box_b
box_in_a_not_b = []
box_in_b_not_a = []
box_match_not_perfect = []
while box_a:
line = box_a.pop()
flag=0
while box_b:
bline = box_b.pop()
if line[0]=bline[0] and line[6]=bline[6] and line[7]=bline[7] # 6 & 7 being PREFIX and PHONE_NUMBER indexes
if not all([line[z]==bline[z] for z in range(len(line))]):
box_match_not_perfect.append(line)
box_match_not_perfect.append(bline) # keeps both instances in the 3d file
else:
box_in_b_not_a.append(bline)
flag =1 # match not found, so add line from A to the file with unique
#end of while box_b
if flag==1:
box_in_a_not_b.append(line)
#end of while box_a
in_a_not_b = [','.join(z) for z in box_in_a_not_b] # to get list of csv lines
# use another '\n'.join() to get one big multiline string, or write line by lie to the file
# to save box_in_a_not_b, box_in_b_not_a and box_in_match_not_perfect in corresponding files
#...