Monday, 15 June 2015

python - Diffing two files while ignoring any missed lines -


I am isolating two duplicate files and my problem is that one of the files gets a row, the other File does not exist, the difference stops I want to write / print the line and after that I will continue. F = open ("output", "w") test_lines =

f1 = open ("file1", "r") f2 = open ("file2", "r" F1.readlines () correct_lines = test for f2.readlines (), correct in zip (sorted (test_lines), sorted (correct_lines)): if test.strip () .split ("(") [0] Replace ("", "") (String () and test! = "\". \ ". (" "," "). N ": Print" Oh no! Expected% R; found% R. "% (Correct, test) Other: write = right + test f.write (write) Other: len_diff = Len (test_lines) - len (correct_lines) if Len_diff & gt; 0: print "too much day in test file"

Sample input

P> file1

  jack tom apple orange  

file2

  jack apple ape mic  

According to your comment, you line Do not want line by comparison. I think Python's set is most appropriate for your case. This is a snippet:

  import re f1 = open ("file1", "r") f2 = open ("file2", "r") f = open ("output", " W ") test_lines = f1.readlines () correct_lines = f2.readlines () test_lines = set ([l.strip (). Partition (" (") [0] .replace (" "," "). Strip () For test_lines)] correct_lines = set ([l.strip (). Split ("(") [0]. ("", ""). (Strip for L in right_lines)) Print "expected:" Expected: Set (['Mike', 'App']), "Correct" and "Got: Set (['orange', 'Tom'])  

No comments:

Post a Comment