The last line of my file is:
我文件的最后一行是:
29-dez,40,
How can I modify that line so that it reads:
如何修改该行,使其显示为:
29-Dez,40,90,100,50
Note: I don't want to write a new line. I want to take the same line and put new values after 29-Dez,40,
注意:我不想写新的一行。我想采取相同的路线,并在29-Dez,40,
I'm new at python. I'm having a lot of trouble manipulating files and for me every example I look at seems difficult.
我是python的新手。我在操作文件时遇到了很多麻烦,对我而言,我看到的每个例子都很难。
4 个解决方案
#1
5
Unless the file is huge, you'll probably find it easier to read the entire file into a data structure (which might just be a list of lines), and then modify the data structure in memory, and finally write it back to the file.
除非文件很大,否则您可能会发现将整个文件读入数据结构(可能只是行列表)更容易,然后修改内存中的数据结构,最后将其写回文件。
On the other hand maybe your file is really huge - multiple GBs at least. In which case: the last line is probably terminated with a new line character, if you seek to that position you can overwrite it with the new text at the end of the last line.
另一方面,你的文件可能非常庞大 - 至少有多个GB。在这种情况下:最后一行可能以新行字符结束,如果您寻找该位置,则可以使用最后一行末尾的新文本覆盖它。
So perhaps:
f = open("foo.file", "wb")
f.seek(-len(os.linesep), os.SEEK_END)
f.write("new text at end of last line" + os.linesep)
f.close()
(Modulo line endings on different platforms)
(不同平台上的Modulo行结尾)
#2
3
To expand on what Doug said, in order to read the file contents into a data structure you can use the readlines() method of the file object.
为了扩展Doug所说的内容,为了将文件内容读入数据结构,您可以使用文件对象的readlines()方法。
The below code sample reads the file into a list of "lines", edits the last line, then writes it back out to the file:
下面的代码示例将文件读入“行”列表,编辑最后一行,然后将其写回文件:
#!/usr/bin/python
MYFILE="file.txt"
# read the file into a list of lines
lines = open(MYFILE, 'r').readlines()
# now edit the last line of the list of lines
new_last_line = (lines[-1].rstrip() + ",90,100,50")
lines[-1] = new_last_line
# now write the modified list back out to the file
open(MYFILE, 'w').writelines(lines)
If the file is very large then this approach will not work well, because this reads all the file lines into memory each time and writes them back out to the file, which is very inefficient. For a small file however this will work fine.
如果文件非常大,那么这种方法将无法正常工作,因为每次将所有文件行读入内存并将它们写回文件,这是非常低效的。对于一个小文件,但这将工作正常。
#3
0
Don't work with files directly, make a data structure that fits your needs in form of a class and make read from/write to file methods.
不要直接使用文件,以类的形式创建符合您需求的数据结构,并对文件方法进行读/写。
#4
0
I recently wrote a script to do something very similar to this. It would traverse a project, find all module dependencies and add any missing import statements. I won't clutter this post up with the entire script, but I'll show how I went about modifying my files.
我最近写了一个脚本来做一些非常类似的事情。它将遍历项目,查找所有模块依赖项并添加任何缺少的import语句。我不会在整篇文章中混淆这篇文章,但我将展示如何修改我的文件。
import os
from mmap import mmap
def insert_import(filename, text):
if len(text) < 1:
return
f = open(filename, 'r+')
m = mmap(f.fileno(), os.path.getsize(filename))
origSize = m.size()
m.resize(origSize + len(text))
pos = 0
while True:
l = m.readline()
if l.startswith(('import', 'from')):
continue
else:
pos = m.tell() - len(l)
break
m[pos+len(text):] = m[pos:origSize]
m[pos:pos+len(text)] = text
m.close()
f.close()
Summary: This snippet takes a filename and a blob of text to insert. It finds the last import statement already present, and sticks the text in at that location.
摘要:此代码段需要插入文件名和一串文本。它找到最后一个导入语句,并将文本粘贴到该位置。
The part I suggest paying most attention to is the use of mmap. It lets you work with files in the same manner you may work with a string. Very handy.
我建议最关注的部分是使用mmap。它允许您以与使用字符串相同的方式处理文件。非常便利。
#1
5
Unless the file is huge, you'll probably find it easier to read the entire file into a data structure (which might just be a list of lines), and then modify the data structure in memory, and finally write it back to the file.
除非文件很大,否则您可能会发现将整个文件读入数据结构(可能只是行列表)更容易,然后修改内存中的数据结构,最后将其写回文件。
On the other hand maybe your file is really huge - multiple GBs at least. In which case: the last line is probably terminated with a new line character, if you seek to that position you can overwrite it with the new text at the end of the last line.
另一方面,你的文件可能非常庞大 - 至少有多个GB。在这种情况下:最后一行可能以新行字符结束,如果您寻找该位置,则可以使用最后一行末尾的新文本覆盖它。
So perhaps:
f = open("foo.file", "wb")
f.seek(-len(os.linesep), os.SEEK_END)
f.write("new text at end of last line" + os.linesep)
f.close()
(Modulo line endings on different platforms)
(不同平台上的Modulo行结尾)
#2
3
To expand on what Doug said, in order to read the file contents into a data structure you can use the readlines() method of the file object.
为了扩展Doug所说的内容,为了将文件内容读入数据结构,您可以使用文件对象的readlines()方法。
The below code sample reads the file into a list of "lines", edits the last line, then writes it back out to the file:
下面的代码示例将文件读入“行”列表,编辑最后一行,然后将其写回文件:
#!/usr/bin/python
MYFILE="file.txt"
# read the file into a list of lines
lines = open(MYFILE, 'r').readlines()
# now edit the last line of the list of lines
new_last_line = (lines[-1].rstrip() + ",90,100,50")
lines[-1] = new_last_line
# now write the modified list back out to the file
open(MYFILE, 'w').writelines(lines)
If the file is very large then this approach will not work well, because this reads all the file lines into memory each time and writes them back out to the file, which is very inefficient. For a small file however this will work fine.
如果文件非常大,那么这种方法将无法正常工作,因为每次将所有文件行读入内存并将它们写回文件,这是非常低效的。对于一个小文件,但这将工作正常。
#3
0
Don't work with files directly, make a data structure that fits your needs in form of a class and make read from/write to file methods.
不要直接使用文件,以类的形式创建符合您需求的数据结构,并对文件方法进行读/写。
#4
0
I recently wrote a script to do something very similar to this. It would traverse a project, find all module dependencies and add any missing import statements. I won't clutter this post up with the entire script, but I'll show how I went about modifying my files.
我最近写了一个脚本来做一些非常类似的事情。它将遍历项目,查找所有模块依赖项并添加任何缺少的import语句。我不会在整篇文章中混淆这篇文章,但我将展示如何修改我的文件。
import os
from mmap import mmap
def insert_import(filename, text):
if len(text) < 1:
return
f = open(filename, 'r+')
m = mmap(f.fileno(), os.path.getsize(filename))
origSize = m.size()
m.resize(origSize + len(text))
pos = 0
while True:
l = m.readline()
if l.startswith(('import', 'from')):
continue
else:
pos = m.tell() - len(l)
break
m[pos+len(text):] = m[pos:origSize]
m[pos:pos+len(text)] = text
m.close()
f.close()
Summary: This snippet takes a filename and a blob of text to insert. It finds the last import statement already present, and sticks the text in at that location.
摘要:此代码段需要插入文件名和一串文本。它找到最后一个导入语句,并将文本粘贴到该位置。
The part I suggest paying most attention to is the use of mmap. It lets you work with files in the same manner you may work with a string. Very handy.
我建议最关注的部分是使用mmap。它允许您以与使用字符串相同的方式处理文件。非常便利。