簡體   English   中英

比較兩個文本文件並保存輸出匹配

[英]compare two text file and save the output match

我有兩個文本文件,我想比較它們並將匹配的列保存到新的文本文件中。

文件1:

114.74721

114.85107

2.96667

306.61756

文件2:

115.06603 0.00294 5.90000

114.74721 0.00674 5.40000

114.85107 0.00453 6.20000

111.17744 0.00421 5.50000

192.77787 0.03080 3.20000

189.70226 0.01120 5.00000

0.46762 0.00883 3.70000

2.21539 0.01290 3.50000

2.96667 0.01000 3.60000

5.43310 0.00393 5.50000

0.28537 0.00497 5.10000

308.82348 0.00183 6.60000

306.61756 0.00359 5.20000

我希望輸出為:

114.74721 0.00674 5.40000

114.85107 0.00453 6.20000

2.96667 0.01000 3.60000

306.61756 0.00359 5.20000

我使用了腳本,但是出了點問題,因為輸出文件的行數比應該相同的file1多。您能幫我嗎?

file1=open("file1.txt","r")
file2=open("file2.txt","r")
file3=open("output.txt","w")
  for line1 in file1.readlines():
    file2.seek(0)
    for line2 in file2.readlines():
      if line1.strip() in line2:
        file3.writerow(line2)

編輯

來自file1.txt

114.74721

114.85107

2.96667

306.61756

152.70581

150.04497

91.41869

91.41869

91.73398

92.35076

117.68963

117.69291

115.97827

168.14476

169.94404

73.00571

156.02833

156.02833

來自file3.txt

114.74721 0.00674 5.40000

114.85107 0.00453 6.20000

2.96667 0.01000 3.60000

306.61756 0.00359 5.20000

152.70581 0.02780 2.70000

150.04497 0.00211 6.00000

91.41869 0.00500 3.70000

91.73398 0.00393 4.30000

92.35076 0.00176 5.80000

117.68963 0.15500 2.20000

117.69291 0.15100 2.50000

115.97827 0.00722 7.80000

168.14476 0.00383 5.50000

169.94404 0.00539 4.80000

73.00571 0.00876 3.80000

156.02833 0.00284 6.30000

156.64645 0.01290 3.50000

156.65070 0.02110 4.40000

如果您看到第7行和第8行在file1.txt中具有相同的值91.41869,但在file3.txt中僅提及第7行而不是8。在第17和18行中也是如此。

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)

或者,如果它們必須處於相同的順序,

with open(FILE1) as inf:
    items = [line.strip() for line in inf]
    match = {val:i for i,val in enumerate(items)}
    outp  = ['\n'] * len(items)

with open(FILE2) as inf, open(OUTPUT, "w") as outf:
    for line in inf:
        val = line.split(' ', 1)[0]
        try:
            outp[match[val]] = line
        except KeyError:
            pass
    outf.write(''.join(outp))

請注意,第一個版本將寫出找到的盡可能多的匹配項-如果FILE2中的兩行以“ 114.74721”開頭,您將同時獲得這兩行-而第二個版本將僅保留找到的最后一個匹配項。

暫無
暫無

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

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