简体   繁体   中英

How to compare two files' strings without numbers

I have two files, file1 contains contents as

aaa  
bbb  
ccc

and file 2 contains contents as

ccc 2
ddd 10
eee 11
aaa 12
rrr 3
bbb 20
nnn 46

I would like to do like this, if file2 contains file1's line, then that line would be removed from file2. At last, file2 will be as

ddd 10
eee 11
rrr 3
nnn 46

Besides, my code is

f1 = open("test1.txt","r")
f2 = open("test2.txt","r")

fileOne = f1.readlines()
fileTwo = f2.readlines()
f1.close()
f2.close()
outFile = open("test.txt","w")
x = 0
for i in fileOne:
    if i !=  fileTwo[x]:
    outFile.writelines(fileTwo[x])
    x += 1

outFile.close()

Thank you.

A set is best here:

with open(file1) as f1:
    s = set(x.strip() for x in f1)

with open(file2) as f2, open(fileout,'w') as fout:
    for line in f2:
        if line.split(None,1)[0] not in s:
            fout.write(line)

Assuming your code is working, just replace:

if i !=  fileTwo[x]

by

if not i in fileTwo[x]

You can use startswith() too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM