如何在python中删除文件的一部分? [英] How to delete parts of a file in python?

查看:565
本文介绍了如何在python中删除文件的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在,我要删除空行上方的内容(包括空行本身). 我怎么能用Python的方式做到这一点?

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?

推荐答案

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

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

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

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)

这是一些更简单的版本,对于较旧的Python版本则没有with:

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)

请注意,这可能非常脆弱,例如Windows通常将\r\n用作单个换行符,因此空行应改为\r\n\r\n.但是通常您知道文件的格式仅使用一种换行符,因此可能会很好.

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.

这篇关于如何在python中删除文件的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆