在 Python 中的现有文件前面添加一行
我需要在文本文件的第一行添加一行,看起来我唯一可用的选项是比我期望的 python 代码行更多的代码。类似这样:
f = open('filename','r')
temp = f.read()
f.close()
f = open('filename', 'w')
f.write("#testfirstline")
f.write(temp)
f.close()
有没有更简单的方法?此外,我看到这个两个句柄的示例比打开单个句柄进行读写(“r+”)更频繁 - 这是为什么?
I need to add a single line to the first line of a text file and it looks like the only options available to me are more lines of code than I would expect from python. Something like this:
f = open('filename','r')
temp = f.read()
f.close()
f = open('filename', 'w')
f.write("#testfirstline")
f.write(temp)
f.close()
Is there no easier way? Additionally, I see this two-handle example more often than opening a single handle for reading and writing ('r+') - why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
Python 使很多事情变得简单,并包含许多常见操作的库和包装器,但目标不是隐藏基本事实。
您在这里遇到的基本事实是,您通常无法在不重写整个结构的情况下将数据添加到现有的平面结构中。无论语言如何,这都是事实。
有多种方法可以保存文件句柄或使代码可读性较差,其中许多方法在其他答案中提供,但没有改变基本操作:您必须读入现有文件,然后写出要添加的数据,然后您读入的现有数据。
无论如何都要保存文件句柄,但不要试图将此操作打包到尽可能少的代码行中。事实上,永远不要去寻找最少的代码行——那是混淆,而不是编程。
Python makes a lot of things easy and contains libraries and wrappers for a lot of common operations, but the goal is not to hide fundamental truths.
The fundamental truth you are encountering here is that you generally can't prepend data to an existing flat structure without rewriting the entire structure. This is true regardless of language.
There are ways to save a filehandle or make your code less readable, many of which are provided in other answers, but none change the fundamental operation: You must read in the existing file, then write out the data you want to prepend, followed by the existing data you read in.
By all means save yourself the filehandle, but don't go looking to pack this operation into as few lines of code as possible. In fact, never go looking for the fewest lines of code -- that's obfuscation, not programming.
我会坚持分开读取和写入,但我们当然可以更简洁地表达每一个:
I would stick with separate reads and writes, but we certainly can express each more concisely:
其他方法:
或单行:
感谢您有机会思考这个问题:)
干杯
Other approach:
or a one liner:
Thanks for the opportunity to think about this problem :)
Cheers
您可以这样保存一个写入调用:
当使用“r+”时,您必须在读取之后和写入之前倒带文件。
You can save one write call with this:
When using 'r+', you would have to rewind the file after reading and before writing.
这是一个 3 衬垫,我认为它清晰且灵活。它使用 list.insert 函数,因此如果您确实想在文件前面添加,请使用 l.insert(0, 'insert_str')。当我实际为正在开发的 Python 模块执行此操作时,我使用了 l.insert(1, 'insert_str') 因为我想跳过 '# --coding: utf-8 --'第 0 行的字符串。这是代码。
Here's a 3 liner that I think is clear and flexible. It uses the list.insert function, so if you truly want to prepend to the file use l.insert(0, 'insert_str'). When I actually did this for a Python Module I am developing, I used l.insert(1, 'insert_str') because I wanted to skip the '# -- coding: utf-8 --' string at line 0. Here is the code.
这无需将整个文件读入内存即可完成工作,尽管它可能无法在 Windows 上运行
This does the job without reading the whole file into memory, though it may not work on Windows
一种可能性如下:
One possibility is the following:
如果您希望在文件中添加特定文本之后,则可以使用下面的函数。
因此,首先打开文件,读取它并将其全部保存到一个字符串中。
然后我们尝试在字符串中查找将发生注入的字符数。然后,通过一次写入和字符串的一些智能索引,我们现在可以重写整个文件,包括注入的文本。
If you wish to prepend in the file after a specific text then you can use the function below.
So first you open the file, read it and save it all into one string.
Then we try to find the character number in the string where the injection will happen. Then with a single write and some smart indexing of the string we can rewrite the whole file including the injected text now.
我是否没有看到某些内容,或者我们不能使用足够大的缓冲区来读入输入文件部分(而不是整个内容),并使用此缓冲区遍历文件当它打开时并继续交换文件<->缓冲区内容?
这似乎比读取内存中的整个内容、在内存中修改要高效得多(尤其是对于大文件) > 并将其写回同一个文件或(更糟糕的是)不同的文件。抱歉,现在我没有时间实现示例代码片段,我稍后会再讨论这个问题,但也许您已经明白了。
Am I not seeing something or couldn't we just use a buffer large-enough to read-in the input file in parts (instead of the whole content) and with this buffer traverse the file while it is open and keep exchanging file<->buffer contents?
This seems much more efficient (for big files especially) than reading the whole content in memory, modifying it in memory and writing it back to the same file or (even worse) a different one. Sorry that now I don't have time to implement a sample snippet, I'll get back to this later, but maybe you get the idea.
正如我在这个答案中所建议的,您可以使用以下方法来完成此操作:
As I suggested in this answer, you can do it using the following:
如果你像这样重写它:
它相当短且简单。
对于“r+”,该文件需要已经存在。
If you rewrite it like this:
It's rather short and simple.
For 'r+' the file needs to exist already.
这对我有用
this worked for me
使用操作系统库怎么样?
在这种特殊情况下,使用 Linux 操作系统并使用 sed 函数
这将在指定的
file_name
位置创建一个新行,并插入header
字符串。该过程不会重写整个文件结构,并且可以处理大文件。What about using OS library.
In this particular case with OS Linux and using the sed function
This will create a new line into the specified
file_name
location and insert theheader
string. This process does not rewrite the whole file structure and can process big files.使用
pathlib.Path
read_text()
和write_text()
添加到简明地归档:Use
pathlib.Path
read_text()
andwrite_text()
to prepend to a file concisely: