【问题标题】:compare two text file and save the output match比较两个文本文件并保存输出匹配
【发布时间】:2014-03-09 22:55:12
【问题描述】:

我有两个文本文件,我想比较它们并将匹配的列保存为新文本 文件。

文件 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

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

【问题讨论】:

    标签: python file file-io


    【解决方案1】:
    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”开头,您将同时得到它们 - 而第二个版本将只保留找到的最后一个匹配项。

    【讨论】:

    • 第一个代码给了我小于 file1 的输出文件,第二个代码给了我一个输出文件,它的行数与 file 相同,但有很多空白行。
    • FILE1 和 FILE2 中的搜索值是否相同?我正在做字符串比较,所以“114.74721”!=“114.747210”。第二种情况的输出文件中的空行对应于 FILE1 中未在 FILE2 中找到的值。
    • 是的,它们是相同的。 file1 中的第一行值出现在输出文件中的许多空白行之后。因此,空行与 file1 中未在 file2 中找到的值不对应。缺失值是file1中重复值的值,它只写出输出文件中重复值的一个值。
    • 接下来我应该做什么?
    • 你能把文件 2 中应该匹配但不匹配的几行和文件 1 中的相应行贴出来吗?
    猜你喜欢
    • 1970-01-01
    • 2014-02-20
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多