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

发布于 2024-10-12 04:58:37 字数 209 浏览 5 评论 0原文

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

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

我位于空行下方。
我是一条线。
这里有更多行。

现在,我想删除空行上方的内容(包括空行本身)。 我怎样才能以 Pythonic 的方式做到这一点?

I have a file named a.txt which looks like this:

I'm the first line
I'm the second line.
There may be more lines here.

I'm below an empty line.
I'm a line.
More lines here.

Now, I want to remove the contents above the empty line(including the empty line itself).
How could I do this in a Pythonic way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

何止钟意 2024-10-19 04:58:37

基本上,您无法从文件开头删除内容,因此您必须写入新文件。

我认为Pythonic方式看起来像这样:

# 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)

这里有一些更简单的版本,对于旧的Python版本没有with

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 。但通常您知道文件的格式仅使用一种换行符,因此这可能没问题。

Basically you can't delete stuff from the beginning of a file, so you will have to write to a new file.

I think the pythonic way looks like this:

# 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)

Here are some simpler versions of this, without with for older Python versions:

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

and a very straight forward version that simply splits the file at the empty line:

# 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)

Note that this can be pretty fragile, for example windows often uses \r\n as a single linebreak, so a empty line would be \r\n\r\n instead. But often you know the format of the file uses one kind of linebreaks only, so this could be fine.

大姐,你呐 2024-10-19 04:58:37

简单的方法是从上到下逐一迭代文件中的行:

#!/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

Naive approach by iterating over the lines in the file one by one top to bottom:

#!/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
少女净妖师 2024-10-19 04:58:37

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)

The fileinput module (from the standard library) is convenient for this kind of thing. It sets things up so you can act as though your are editing the file "in-place":

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)
溇涏 2024-10-19 04:58:37

知道文件有多大吗?

您可以将文件读入内存:

f = open('your_file', 'r')
lines = f.readlines()

它将逐行读取文件并将这些行存储在列表(行)中。

然后,关闭文件并使用“w”重新打开:

f.close()
f = open('your_file', 'w')
for line in lines:
    if your_if_here:
        f.write(line)

这将覆盖当前文件。然后,您可以从列表中选择要写回的行。不过,如果文件变得很大,这可能不是一个好主意,因为整个文件必须驻留在内存中。但是,它不需要您创建第二个文件来转储输出。

Any idea how big the file is going to be?

You could read the file into memory:

f = open('your_file', 'r')
lines = f.readlines()

which will read the file line by line and store those lines in a list (lines).

Then, close the file and reopen with 'w':

f.close()
f = open('your_file', 'w')
for line in lines:
    if your_if_here:
        f.write(line)

This will overwrite the current file. Then you can pick and choose which lines from the list you want to write back in. Probably not a very good idea if the file gets to large though, since the entire file has to reside in memory. But, it doesn't require that you create a second file to dump your output.

陌生 2024-10-19 04:58:37
from itertools import dropwhile, islice

def content_after_emptyline(file_object):
    return islice(dropwhile(lambda line: line.strip(), file_object), 1, None)

with open("filename") as f:
    for line in content_after_emptyline(f):
        print line,
from itertools import dropwhile, islice

def content_after_emptyline(file_object):
    return islice(dropwhile(lambda line: line.strip(), file_object), 1, None)

with open("filename") as f:
    for line in content_after_emptyline(f):
        print line,
李不 2024-10-19 04:58:37

你可以做一些像这样的事情:

with open('a.txt', 'r') as file:
    lines = file.readlines()

blank_line = lines.index('\n')
lines = lines[blank_line+1:] #\n is the index of the blank line

with open('a.txt', 'w') as file:
    file.write('\n'.join(lines))

这会让工作变得更简单。

You could do a little something like this:

with open('a.txt', 'r') as file:
    lines = file.readlines()

blank_line = lines.index('\n')
lines = lines[blank_line+1:] #\n is the index of the blank line

with open('a.txt', 'w') as file:
    file.write('\n'.join(lines))

and that makes the job much simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文