Advertisement
trodland

Help - python compare files

Apr 6th, 2020
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. file1 = "file1.txt"
  2. file2 = "file2.txt"
  3. outputfile = "file3.txt"
  4.  
  5. f1lines = []
  6. f2stripped = []
  7. f2linesoriginal = []
  8. output = []
  9.  
  10. with open(file1, "r") as f1:
  11.     for line in f1:
  12.         f1lines.append(line.strip())
  13.    
  14. with open(file2, "r") as f2:
  15.     for line in f2:
  16.         f2linesoriginal.append(line)
  17.         compdata = line.split(";")[-2:]   #removing the [ag] in start of line.....
  18.         datastring = ""
  19.         for element in compdata:
  20.             datastring += element.strip() + ";" #putting the string back toghether adding ";"
  21.            
  22.         f2stripped.append(datastring[:-1])
  23.  
  24. for line in f1lines:
  25.     if line in f2stripped:
  26.         output.append(f2linesoriginal[f2stripped.index(line)].strip()) #if match add the original f2 line
  27.     else:
  28.         output.append(line) # else keep the f1 line
  29.        
  30. print(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement