简体   繁体   中英

comparing two text files and output in third file

I have two files on with list of VRM and other which contains images with that name.

This is format : text file 1:

A10KLG
M10SNW
N12TPC
P03GRI

TEXT FILE 2 : the strucure of the file DecathlonIN/2015-03-12/; SmythOUT/2015-03-12/

DecathlonIN/2015-03-12/ - this is in text file 2

inside todays date folder are these images.

    A10KLG-GBR_DecathlonIN_2015-03-12_10-52-33-152.jpg
    M10SNW-GBR_DecathlonIN_2015-03-12_11-58-35-162.jpg
    N12TPC-GBR_DecathlonIN_2015-03-12_14-51-33-152.jpg
    SKLG3-GBR_DecathlonIN_2015-03-12_10-52-33-152.jpg
    K10SNW-GBR_DecathlonIN_2015-03-12_10-52-33-152.jpg
    ST2TPC-GBR_DecathlonIN_2015-03-12_10-52-33-152.jpg

I need to find any any vrm which matches in the third file.

this is my code:

    FILE1 = "file1.txt"
    FILE2 = "file2.txt"
    OUTPUT = "file3.txt"

    with open(FILE1) as inf:
        match = set(line.strip() for line in inf)

    with open(FILE2) as inf, open(OUTPUT, "w") as outf:
        for line in inf:
            if line.split(' ', 1)[0] in match:
                outf.write(line)-----

Like A10KLG is matched in two files it should bring up that in another file...

A10KLG-GBR_DecathlonIN_2015-03-12_10-52-33-152.jpg

Go for the easiest split that's the same between all of your data: - .

FILE1 = "file1.txt"
FILE2 = "file2.txt"
OUTPUT = "file3.txt"

with open(FILE1) as inf:
    match = set(line.strip() for line in inf)

with open(FILE2) as inf, open(OUTPUT, "w") as outf:
    for line in inf:
        if line.split('-')[0] in match:
            outf.write(line)

file3.txt

A10KLG-GBR_DecathlonIN_2015-03-12_10-52-33-152.jpg
M10SNW-GBR_DecathlonIN_2015-03-12_11-58-35-162.jpg
N12TPC-GBR_DecathlonIN_2015-03-12_14-51-33-152.jpg

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