randomly write to a file
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Mon May 7 23:35:49 EDT 2007
On Mon, 07 May 2007 20:00:57 -0700, Alex Martelli wrote:
> Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> wrote:
>
>> On Mon, 07 May 2007 14:41:02 -0700, Nick Vatamaniuc wrote:
>>
>> > 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.
>>
>>
>> Using an database for tracking line number and byte position -- isn't
>> that a bit overkill?
>>
>> I would have thought something as simple as a list of line lengths
>> would do:
>>
>> offsets = [35, # first line is 35 bytes long
>> 19, # second line is 19 bytes long... 45, 12, 108, 67]
>>
>>
>> To get to the nth line, you have to seek to byte position:
>>
>> sum(offsets[:n])
>
> ...and then you STILL can't write there (without reading and rewriting
> all the succeeding part of the file) unless the line you're writing is
> always the same length as the one you're overwriting, which doesn't seem
> to be part of the constraints in the OP's original application. I'm
> with Nick in recommending SQlite for the purpose -- it _IS_ quite
> "lite", as its name suggests.
Hang on, as I understand it, Nick just suggesting using SQlite for
holding indexes into the file! That's why I said it was overkill. So
whether the indexes are in a list or a database, you've _still_ got to
deal with writing to the file.
If I've misunderstood Nick's suggestion, if he actually meant to read the
entire text file into the database, well, that's just a heavier version
of reading the file into a list of strings, isn't it? If the database
gives you more and/or better functionality than file.readlines(), then I
have no problem with using the right tool for the job.
--
Steven.
More information about the Python-list
mailing list