【问题标题】:How can I delete "\n" lines from a file in Python?如何从 Python 中的文件中删除“\n”行?
【发布时间】:2019-10-06 20:49:51
【问题描述】:

我需要检查我正在使用的 .csv 文件是否以超过 1 个 "\n" 行结尾。如果它发现多于一个空行,则将它们全部删除,只留下一个。

我的代码是:

import os
from pathlib import Path


def remove_blanks():
    dirname = os.path.dirname(os.path.abspath(__file__))
    path: Path = Path(os.path.join(dirname, "data.csv"))
    with open(path, "r+") as op:
        lines = op.readlines()
        for line in lines:
            if line == "\n":
                op.write(line.rstrip("\n"))

.csv 文件类似于['01-01-2019,0,0,0\n', '18-05-2019,33,31,48\n', '\n', '\n', '\n'],我想要的输出是['01-01-2019,0,0,0\n', '18-05-2019,33,31,48\n', '\n'],但它似乎无法删除任何行。

【问题讨论】:

  • 您想从 any 序列中删除多余的空行,还是只删除文件末尾的空行? (这会有所不同,因为在您真正到达文件末尾之前,您不会知道序列出现在文件末尾,因此您无法决定写入或立即跳过。)
  • @chepner 我只对文件末尾感兴趣。

标签: python csv readlines


【解决方案1】:

最简单的方法是在您看到 an 空行时进行跟踪,然后在您编写 -空行之前写一个。

pre = ""
for line in lines:
    if line == "\n":
        pre = line
    else:
        op.write(pre)
        op.write(line)
        pre = "\n"
op.write(pre)

这会将任何空行序列减少为单个空行,并在写入非空行或文件末尾之前写入该单行。当pre 为空字符串时,写入是空操作。

如果要在文件中间保留多个空行,请在找到它们时在pre中建立空行序列,并在文件末尾只写一个空行(而不是比pre 本身)如果pre 不为空。

pre = ""
for line in lines:
    if line == "\n":
        pre += line
    else:
        op.write(pre)
        op.write(line)
        pre = ""
if pre:
    op.write("\n")

【讨论】:

    【解决方案2】:

    糟糕,永远不要重写您正在阅读的文件:它可能无法正常工作,或者充其量只会导致维护噩梦。

    如果文件足够小,可以放入主内存,那么对代码稍作改动就足够了:

    import os.path
    from pathlib import Path
    
    
    def remove_blanks():
        dirname = os.path.dirname(os.path.abspath(__file__))
        path: Path = Path(os.path.join(dirname, "data.csv"))
        with open(path, "r") as op:
            lines = op.readlines()  # read lines in memory
        with open(path("w") as op:  # re-write everything from the beginning
            flag = False     
            for line in lines:
                if line == "\n":
                    if not flag:
                        op.write(line)
                    flag = True
                else:
                    op.write(line)
                    # flag = False  # uncomment if you want to keep one blank line 
                                    # per group of consecutive lines
    

    【讨论】:

    • 感谢有关文件打开的提示!我现在已经尝试过了,但无论文件末尾是什么,它似乎都会留下 2 个连续的 \n
    【解决方案3】:

    您可以尝试使用Counter()

    import os
    from pathlib import Path
    from collections import Counter
    
    def remove_blanks():
        dirname = os.path.dirname(os.path.abspath(__file__))
        path: Path = Path(os.path.join(dirname, "data.csv"))
        with open(path, "r+") as op:
            lines = op.readlines()
            for line in lines:
                count = Counter()
                # Add 1 for every time word appears in line
                for word in line:
                    count[word] += 1
                # Change the number of newlines to 1
                if count['\n'] > 1:
                    count['\n'] = 1
                # Returns list with the number of elements
                line = list(count.elements())
    

    【讨论】:

      【解决方案4】:

      我设法用这个代码解决了这个问题:

      import os
      from pathlib import Path
      
      
      def remove_blanks():
          dirname = os.path.dirname(os.path.abspath(__file__))
          path: Path = Path(os.path.join(dirname, "data.csv"))
          with open(path, "r") as op:
              lines = op.readlines()  # read lines in memory
          with open(path, "w") as op: # re-write everything from the beginning
              for line in lines:
                  if line != "\n":
                      op.write(line)
                  else:
                      continue
      

      它可以删除每个多余的新行,无论它在文件中的什么位置。

      感谢所有试图帮助我的人!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-11
        • 2019-05-31
        • 1970-01-01
        • 2014-12-14
        相关资源
        最近更新 更多