随机写入文件 [英] randomly write to a file

查看:65
本文介绍了随机写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i正在开发一个桌面搜索。对于我拥有的文件的索引

开发了一个算法,其中

i应该是能够读取和写入一行,如果我知道它的行

数。

i可以使用模块linecache读取指定的行

但是我很惊讶如何在

文件中实现写入n(th)行有效地

这意味着我不想按顺序遍历文件到达

第n行


请帮忙。

问候

Rohit


i am developing a desktop search.For the index of the files i have
developed an algorithm with which
i should be able to read and write to a line if i know its line
number.
i can read a specified line by using the module linecache
but i am struck as to how to implement writing to the n(th) line in a
file EFFICIENTLY
which means i don''t want to traverse the file sequentially to reach
the n(th) line

Please help.
Regards
Rohit

推荐答案

5月7日下午2:51,rohit< rohitsethi ... @ gmail.comwrote:
On May 7, 2:51 pm, rohit <rohitsethi...@gmail.comwrote:

hi,

i正在开发一个桌面搜索。对于我拥有的文件的索引

开发了一个算法,其中

i应该能够读取和写入一行如果我知道它的行

数。

i可以读取一个spe通过使用模块linecache确定线路

但我很惊讶如何在

文件中实现写入第n行有效

这意味着我不想按顺序遍历文件到达

第n行


请帮忙。

问候

Rohit

i am developing a desktop search.For the index of the files i have
developed an algorithm with which
i should be able to read and write to a line if i know its line
number.
i can read a specified line by using the module linecache
but i am struck as to how to implement writing to the n(th) line in a
file EFFICIENTLY
which means i don''t want to traverse the file sequentially to reach
the n(th) line

Please help.
Regards
Rohit






浏览档案,它看起来有些人建议将

文件读入列表并按此方式执行。如果他们的文件太大,那么比使用数据库要好b $ b。请参阅以下链接:

http://mail.python.org/pipermail/tut...ch/045571.html
http://mail.python.org/pipermail/tut...ch/045572.html


我还发现了这个有趣的想法,解释了完成这项任务需要什么


http://mail.python.org/pipermail/pyt...il/076890.html


玩得开心!


Mike

Hi,

Looking through the archives, it looks like some recommend reading the
file into a list and doing it that way. And if they file is too big,
than use a database. See links below:

http://mail.python.org/pipermail/tut...ch/045571.html
http://mail.python.org/pipermail/tut...ch/045572.html

I also found this interesting idea that explains what would be needed
to accomplish this task:

http://mail.python.org/pipermail/pyt...il/076890.html

Have fun!

Mike


En星期一,2007年5月7日16:51:37 -0300,rohit< ro *********** @ gmail.com>
$ b $bescribió:
En Mon, 07 May 2007 16:51:37 -0300, rohit <ro***********@gmail.com>
escribió:

i正在开发一个桌面搜索。对于我拥有的文件的索引

开发了一个算法如果我知道它的行

数字,那么

i应该能够读取和写入一行。

i可以通过使用读取指定的行模块linecache

但我很惊讶如何在

文件中实现写入第n行的效果

这意味着我不想按顺序遍历文件到达

n(th)行
i am developing a desktop search.For the index of the files i have
developed an algorithm with which
i should be able to read and write to a line if i know its line
number.
i can read a specified line by using the module linecache
but i am struck as to how to implement writing to the n(th) line in a
file EFFICIENTLY
which means i don''t want to traverse the file sequentially to reach
the n(th) line



你只能替换一行-place与另一个完全相同的

长度。如果长度不同,你必须写下修改过的行和所有

以下的行。

如果你的所有行都是固定长度的,你有一个记录。 。阅读记录

N(从0开始计算):

a_file.seek(N * record_length)

返回a_file.read(record_length)

然后你重新发明了ISAM。


-

Gabriel Genellina

You can only replace a line in-place with another of exactly the same
length. If the lengths differ, you have to write the modified line and all
the following ones.
If all your lines are of fixed length, you have a "record". To read record
N (counting from 0):
a_file.seek(N*record_length)
return a_file.read(record_length)
And then you are reinventing ISAM.

--
Gabriel Genellina


Rohit,


考虑使用SQLite数据库。它带有Python 2.5和

更高。 SQLite将很好地跟踪索引。您可以通过SQL查询轻松找到所需的行,并且您也可以写入

。如果你有一个文件并且你写了

文件的一行,那么所有其余的行都必须转移到

容纳,可能更大的新文件线路。


-Nick Vatamaniuc

5月7日下午3:51,rohit< rohitsethi ... @ gmail.comwrote:
Rohit,

Consider using an SQLite database. It comes with Python 2.5 and
higher. SQLite will do a nice job keeping track of the index. You can
easily find the line you need with a SQL query and your can write to
it as well. When you have a file and you write to one line of the
file, all of the rest of the lines will have to be shifted to
accommodate, the potentially larger new line.

-Nick Vatamaniuc
On May 7, 3:51 pm, rohit <rohitsethi...@gmail.comwrote:

hi,

i正在开发一个桌面搜索。对于我拥有的文件的索引

开发了一个算法,用它来<如果我知道它的行

数字,那么
i应该能够读取和写入一行。

i可以使用模块linecache读取指定的行

但我很惊讶如何在

文件中实现写入n(th)行有效

这意味着我要'我想顺序遍历文件到达

第n行


请帮忙。

问候

Rohit

i am developing a desktop search.For the index of the files i have
developed an algorithm with which
i should be able to read and write to a line if i know its line
number.
i can read a specified line by using the module linecache
but i am struck as to how to implement writing to the n(th) line in a
file EFFICIENTLY
which means i don''t want to traverse the file sequentially to reach
the n(th) line

Please help.
Regards
Rohit



这篇关于随机写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆