如何在Python中删除文件的部分内容?

0 投票
6 回答
17535 浏览
提问于 2025-04-16 09:57

我有一个名为 a.txt 的文件,内容如下:

我是第一行
我是第二行。
这里可能还有更多行。

我在一个空行下面。
我是一行。
这里还有更多行。

现在,我想删除空行上面的内容(包括空行本身)。我该如何用 Python 的方式来做到这一点呢?

6 个回答

1

fileinput模块(来自标准库)在处理这种情况时非常方便。它可以让你像是在“原地”编辑文件一样进行操作:

import fileinput
import sys

fileobj=iter(fileinput.input(['a.txt'], inplace=True))
# iterate through the file until you find an empty line.
for line in fileobj:
    if not line.strip():
        break
# Iterators (like `fileobj`) pick up where they left off. 
# Starting a new for-loop saves you one `if` statement and boolean variable.
for line in fileobj:
    sys.stdout.write(line)
2

一种简单的方法是逐行读取文件,从上到下一个一个地处理:

#!/usr/bin/env python

with open("4692065.txt", 'r') as src, open("4692065.cut.txt", "w") as dest:
    keep = False
    for line in src:
        if keep: dest.write(line)
        if line.strip() == '': keep = True
3

基本上,你不能直接从文件的开头删除内容,所以你需要写入一个新文件。

我觉得用Python的方式可以这样做:

# get a iterator over the lines in the file:
with open("input.txt", 'rt') as lines:
    # while the line is not empty drop it
    for line in lines:
        if not line.strip():
            break

    # now lines is at the point after the first paragraph
    # so write out everything from here
    with open("output.txt", 'wt') as out:
        out.writelines(lines)

这里有一些更简单的版本,没有使用with,适合旧版Python:

lines = open("input.txt", 'rt')
for line in lines:
    if not line.strip():
        break
open("output.txt", 'wt').writelines(lines)

还有一个非常简单的版本,它只是根据空行来分割文件:

# first, read everything from the old file
text = open("input.txt", 'rt').read()

# split it at the first empty line ("\n\n")
first, rest = text.split('\n\n',1)

# make a new file and write the rest
open("output.txt", 'wt').write(rest)

需要注意的是,这种方法可能会有点脆弱,比如Windows系统通常使用\r\n作为一个换行符,所以一个空行实际上会是\r\n\r\n。不过通常你知道文件的格式只使用一种换行符,这样就没问题了。

撰写回答