[英]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.