如何在python中删除文件的部分内容?
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基本上,您无法从文件开头删除内容,因此您必须写入新文件。
我认为Pythonic方式看起来像这样:
这里有一些更简单的版本,对于旧的Python版本没有
with
:还有一个非常直接的版本,只是在空行处分割文件:
请注意,可能非常脆弱,例如 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:
Here are some simpler versions of this, without
with
for older Python versions:and a very straight forward version that simply splits the file at the empty line:
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.简单的方法是从上到下逐一迭代文件中的行:
Naive approach by iterating over the lines in the file one by one top to bottom:
fileinput 模块(来自标准库)对于这种情况很方便事物。它会进行设置,以便您可以像正在“就地”编辑文件一样操作:
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":
知道文件有多大吗?
您可以将文件读入内存:
它将逐行读取文件并将这些行存储在列表(行)中。
然后,关闭文件并使用“w”重新打开:
这将覆盖当前文件。然后,您可以从列表中选择要写回的行。不过,如果文件变得很大,这可能不是一个好主意,因为整个文件必须驻留在内存中。但是,它不需要您创建第二个文件来转储输出。
Any idea how big the file is going to be?
You could read the file into memory:
which will read the file line by line and store those lines in a list (lines).
Then, close the file and reopen with 'w':
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.
你可以做一些像这样的事情:
这会让工作变得更简单。
You could do a little something like this:
and that makes the job much simpler.