编辑大文本文件中的单行 [英] Editing a single line in a large text file
问题描述
所以我需要记录一组 4 个整数,它们的值在一天中的每一秒都不同.即:
So I need to record a set of 4 integers whose values are different for every second of the day. i.e.:
#Here the values are initialized to the same value, however they will change as samples are taken
data = [[.25 for numData in range(4)] for numSecs in range(86400)]
显然,第一个索引长度为 86400 的二维数组(gah it python, LIST)是非常不切实际的.相反,我想创建一个 86400 行的文本文件,格式如下:
Now obviously a two dimensional array(gah its python, LIST) whose first index length is 86400 is quite impractical. Instead I want to create a text file with 86400 lines formatted as such:
numSec data0 data1 data2 data3
0 .25 .25 .25 .25
1 .25 .25 .25 .25
2 .25 .25 .25 .25
...
并且在采集样本时,我希望能够编辑此文件,不,我希望能够编辑 numSec = 采集样本的第二行的文件行.例如,在 numSec = 2(午夜后 2 秒)处采集的样本将导致我的程序编辑文件,以便:
And as samples are taken, I want to be able to edit this file, nay, I want to be able to edit the line of the file whose numSec = the second the sample was taken. For example, a sample taken at numSec = 2 (2 seconds after midnight) would cause my program to edit the file so that:
0 .25 .25 .25 .25
1 .25 .25 .25 .25
2 .70 .10 .10 .10
...
似乎很容易,我什至阅读了一堆帖子,这些帖子演示了如何在文本文件中重写单个文件.问题是,它们都要求您阅读整个文件.我不希望我的程序每秒读取 86,400 行.
Seems easy enough, I have even read a bunch of posts which demonstrated how to rewrite a single in a text file. The problem is, they all require that you read in the whole file. I don't want my program to be reading 86,400 lines every second.
因此我们得出了我的问题:我可以读取文本文件中的一行,对其进行编辑,然后将其写回文件,而无需每次需要进行更改时都读取整个文件吗?
Thus we arrive at my question: Can I read a single line in a text file, edit it, and write it back to the file, without reading the entire file every single time a change needs to be made?
附言我应该注意我正在运行 Ubuntu 12.04(精确),这是用于 ROS 节点
P.S. I should note I am running Ubuntu 12.04 (Precise) and this is for use in a ROS node
P.P.S 该程序将运行任意天数,因此可以多次读取和重写每一秒"数据.我想使用文件的另一个原因是,如果系统需要关闭,我想在下次运行时保存发行版.
P.P.S This program will be running for an arbitrary amount of days so each "second" of data could be read and rewritten many times. Also another reason I would like to use a file is if the system needs to be shut off, I would like to save the distributions for the next time it is run.
推荐答案
您可能需要稍微修改一下,它假定所有行的长度相同.为此,我必须将第一列填充到固定宽度.但是,如果您不想填充,您应该能够计算特定行之前的 1、2、3、4、.. 位数.
You may need to modify this a bit and it assume that all lines are of the same length. For this, I had to pad the first column to a fixed width. But if you don't want padding you should be able to calculate the number of 1,2,3,4,.. digit numbers before a particular row.
data = [[.25 for numData in range(4)] for numSecs in range(86400)]
length_line=0
def write_line(f, sec, data):
line="{:6d}".format(sec) + " " +" ".join(
["{:.2f}".format(dd) for dd in data])+"\n"
f.write(line)
return len(line)
with open('output', 'w') as of:
for i,d in enumerate(data):
length_line=write_line(of, i, d)
with open('output', 'rb+') as of:
# modify the 2nd line:
n = 2
of.seek(n*length_line)
write_line(of, n, [0.10,0.10,0.10,0.10])
# modify the 10th line:
n = 10
of.seek(n*length_line)
write_line(of, n, [0.10,0.10,0.10,0.10])
这篇关于编辑大文本文件中的单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!