I am trying to do the following. Compare two text files ( Masterfile and usedfile) and write the unique values(not common in both) of Masterfile to third file (Newdata ). Both files have one word in each line. example:
Masterfile content
Johnny
transfer
hello
kitty
usedfile content
transfer
hello
expected output in Newdata
Johnny
kitty
I have two solutions but both have problem
solution 1:This gives information like -,+ prefixed to the data final output.
import difflib
with open(r'C:\Master_Data.txt','r') as masterfile:
with open(r'C:\Used_Data.txt','r') as usedfile:
with open(r'c:\Ready_to_use.txt','w+') as Newdata:
tempmaster = masterfile.readlines()
tempusedfile = usedfile.readlines()
d = difflib.Differ()
diff = d.compare(tempmaster,tempusedfile)
for line in diff:
Newdata.write(line)
solution 2: I tried using set ,it shows fine when I use print statement but don't know how to write to a file.
with open(r'C:\Master_Data.txt','r') as masterfile:
with open(r'C:\Used_Data.txt','r') as usedfile:
with open(r'c:\Ready_to_use.txt','w+') as Newdata:
difference = set(masterfile).difference(set(usedfile))
print difference
Can anyone suggest
- how I can correct the solution 2 to write to a file.
- can I use difflib to accomplish the task
- Any better solution to achieve the end result