簡體   English   中英

比較兩個文本文件並在第三個文件中輸出

[英]comparing two text files and output in third file

我有兩個文件帶有VRM列表,另一個文件包含具有該名稱的圖像。

這是格式:文本文件1:

A10KLG
M10SNW
N12TPC
P03GRI

TEXT FILE 2:文件DecathlonIN / 2015-03-12 /的結構; SmythOUT / 2015年3月12日/

DecathlonIN / 2015-03-12 /-在文本文件2中

在今天的日期文件夾中是這些圖像。

    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)-----

就像A10KLG在兩個文件中匹配一樣,它應該在另一個文件中顯示它。

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

在所有數據之間進行最簡單的分割: -

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM