【问题标题】:How to remove repeated lines from a file? [duplicate]如何从文件中删除重复的行? [复制]
【发布时间】:2023-03-10 08:55:01
【问题描述】:

我有 one.txt 包含数据的文件:

 822.25 111.48 883.59 256.68
 822.25 111.48 883.59 256.68
 8.6 123.68 467.27 276.69
 0.0 186.77 165.62 375.0
 0.0 186.77 165.62 375.0
 724.76 177.83 923.52 316.78
 724.76 177.83 923.52 316.78
 724.76 177.83 923.52 316.78
 724.76 177.83 923.52 316.78
 724.76 177.83 923.52 316.78
 438.03 148.5 540.88 198.54
 511.99 170.97 571.74 215.81
 511.99 170.97 571.74 215.81

对于重复的行,我只想为它们写一行。例如:

724.76 177.83 923.52 316.78

重复5次,我只想写一次,对其他行也做同样的事情,然后将新数据写入文件。

我的代码:

with open('one.txt', 'r') as infile:
    with open('output.txt', 'w') as outfile:
        for line in infile:
            #how to do this?
            if line are repeated remove and replace them with only one line
               outfile.write(line)

【问题讨论】:

  • 您的意思是“立即重复”还是“在任何地方重复”?对于前者,这是一个以“last_input”作为循环变量的简单循环。对于后者,可以使用排序图来完成,或者,如果您不关心唯一行的顺序,您可以先对行进行排序,然后使用“立即重复”版本的代码。
  • 如果是一次性练习,您可以使用 linux 命令。排序 yourfile.txt | uniq -u

标签: python


【解决方案1】:

这可以通过 linux 实用程序 uniq 完成,只需在终端输入 uniq <infile.txt >outfile.txt。这里符号>< 告诉shell 使用提供的文件而不是标准输入和输出。

要在 python 中重新发明这个实用程序,可以这样写:

with open('one.txt', 'r') as infile:
    with open('output.txt', 'w') as outfile:
        prev_line = infile.readline()  # read first line
        outfile.write(prev_line)
        for line in infile:
            if line != prev_line:  # if the line is a different one, print it
                prev_line = line
                outfile.write(line)

【讨论】:

    【解决方案2】:

    您可能想要itertools.groupby,如果没有比较函数,它只会为每个唯一行返回一个“组”,因此您可以完全跳过该组,只需从每个分组中写入一行。

    with open('one.txt', 'r') as infile:
        with open('output.txt', 'w') as outfile:
            for line, _ in itertools.groupby(infile):
                outfile.write(line)
    

    这只会替换出现在同一区域的组,如果重复的行可能出现在文件中的多个位置(例如a a b a 会写成a b a),那么您可以保留一组您已经看过的行

    seen_lines = set()
    with open('one.txt', 'r') as infile:
        with open('output.txt', 'w') as outfile:
            for line in infile:
                if line in seen_lines:
                    continue
                outfile.write(line)
                seen_lines.add(line)
    

    【讨论】:

      【解决方案3】:

      您可以利用itertools.groupby() 如下:

      from itertools import groupby
      
      data = """
      822.25 111.48 883.59 256.68
       822.25 111.48 883.59 256.68
       8.6 123.68 467.27 276.69
       0.0 186.77 165.62 375.0
       0.0 186.77 165.62 375.0
       724.76 177.83 923.52 316.78
       724.76 177.83 923.52 316.78
       724.76 177.83 923.52 316.78
       724.76 177.83 923.52 316.78
       724.76 177.83 923.52 316.78
       438.03 148.5 540.88 198.54
       511.99 170.97 571.74 215.81
       511.99 170.97 571.74 215.81
      """
      
      ones = [key 
              for key, _ in groupby(
                  (line.strip() for line in data.split("\n") if line)
              )]
      print(ones)
      

      这会产生

      [
       '822.25 111.48 883.59 256.68',
       '8.6 123.68 467.27 276.69', 
       '0.0 186.77 165.62 375.0', 
       '724.76 177.83 923.52 316.78', 
       '438.03 148.5 540.88 198.54', 
       '511.99 170.97 571.74 215.81'
      ]
      

      【讨论】:

        【解决方案4】:

        也许可以尝试做一个 If 语句,如果它被打印一次就可以了,那么也许可以让 Elif 删除任何相似或相同的字符串。

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 2010-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-03
        • 2011-11-08
        • 2012-04-05
        相关资源
        最近更新 更多