scorpionz scorpionz - 3 months ago 13
Python Question

How to compare multiple files in python and show all the name variations associated with SSN's

Filter/sort files to list all names variations associated with each SSN.

Input File1:
SSN, First Name, Last Name
333-22-9898, Tom, Tillman
556-11-7484, Mak, Burhan
333-22-9898, Tom, B Tillman

Input File2:
SSN, First Name, Last Name
857-87-9899, Si, H

Input File 3....

Output File:
333-22-9898, Tom, Tillman
333-22-9898, Tom, B Tillman
556-11-7484, Mak, Burhan
556-11-7484, Mak, Bo
.......and so on....

Answer

Solution:

f = open('out.txt', 'w')
# sample data in f1.txt (111-23-9999)
filenames = ["f1.txt", "f2.txt", "f3.txt", "f4.txt"]
files = [open(name) for name in filenames]

sets = [set(line.strip() for line in file) 
            for file in files]
common = set.union(*sets)

my_list = list(common)
my_list.sort()
print (my_list)
for file in files: file.close()


for line in my_list:
    f.write(line+'\n')
f.close()
Comments