问题描述
我有一个名为a.txt的文件,如下所示:
I have a file named a.txt which looks like this:
我在空白行下方.
我是一行.
这里有更多行.
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中删除文件的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!