jeudi 13 août 2015

Compare lines in two .txt files, print out new line for not contained words

I have the following piece of code that, for every line in textfile1, searches textfile2 and if the line is contained in textfile2 prints out the corresponding line of textfile2. I want to however, print out new line for every line not contained in textfile2. Here is the code:

def readline():
with open("textfile1.txt") as file, open("textfile2.txt") as file2:
    string = set(map(str.rstrip,file))
    for line in file2:
        spl = line.split(None, 1)[0]
        if spl in string:
            print(line.rstrip())    
        else:              ##if spl not in string print new line
            print("\n")

It doesn't work as I expect (doesn't print out any new lines), what may be the problem or any alternative solutions?

Sample Textfile1:

'
a
aa
ab
abandon
abandonaudiofocus
abandonsession
abort
abortablehttprequest
abortanimation
abortcaptures
abortconnection
abortpolicy
abortrequest
abs

Sample Textfile2:

'                |            22624
a                |               91
aa               |                7
ab               |                6
abort            |                8
abortanimation   |                5
abs              |              131
abslistview      |              115
absolutelayout   |               50
absolutesizespan |                6
abstracthttpentity |                2
abstractlist     |                1
abstractmap      |                4
abstractselector |                1
abstractset      |                2

Textfile1 includes many more words and it contains all the words in textfile2.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire