python - Diffing two files while ignoring any missed lines -


i diffing 2 unsorted files , problem moment 1 of files gets line not present in other file, diff goes off. want write/print line not present , continue on.

import re f1=open("file1","r") f2=open("file2","r") f=open("output","w") test_lines=f1.readlines() correct_lines=f2.readlines()  test, correct in zip(sorted(test_lines), sorted(correct_lines)):     if test.strip().split("(")[0].replace(" ","").strip() != correct.strip().split("(")[0].replace(" ","").strip() , test!="\n":         print "oh no! expected %r; got %r." % (correct, test)     else:         towrite=correct + test         f.write(towrite)  else:     len_diff = len(test_lines) - len(correct_lines)     if len_diff > 0:         print "test file had data."     elif len_diff < 0:         print "test file had little data."     else:         print "everything correct!" 

sample input

file1

 jack tom apple orange 

file2

jack apple ape  mike 

it print oh no! expected apple got ape , fail

according comments don't want line line comparison. think python's set best suited case. here 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().split("(")[0].replace(" ","").strip() l in test_lines]) correct_lines = set([l.strip().split("(")[0].replace(" ","").strip() l in correct_lines])  print "expected: ", correct_lines-test_lines     print "got: ", test_lines-correct_lines 

output:

expected:  set(['mike', 'ape']) got:  set(['orange', 'tom']) 

Comments

  1. Hi Admin,

    Automated text messages if your phone detects you're a long way from home, or discounted home internet, are just a few possible technology solutions to make New Zealanders "stay home to save lives".

    Regards,

    Dentist In Chicopee

    ReplyDelete

Post a Comment

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -