write to specific line in file?

castironpi castironpi at gmail.com
Fri May 16 18:57:52 EDT 2008


On May 16, 5:22 pm, castironpi <castiro... at gmail.com> wrote:
> On May 16, 2:25 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
>
>
>
>
>
> > globalrev wrote:
> > > i ahve a program that takes certain textsnippets out of one file and
> > > inserts them into another.
>
> > > problem is it jsut overwrites the first riow every time.
>
> > > i want to insert every new piece of text into the next row.
> > > so:
> > > 1. how do i write to a new line every time i write to the file?
>
> > > 2. if i dont want to write to a new line but just want to insert it
> > > after the last token in the file, how do i do then?
>
> > Generally, you can't "insert" anything into a file.  You can either
> > append to the end of a file, or you can rewrite the whole file.  It
> > sounds like you probably want to read the file into an array using
> > readlines().  Then manipulate that array however you want--appending
> > and inserting--and when you are done, overwrite the file with your
> > array.
>
> > However, you should be aware that as soon as you open a file for
> > writing all the data is erased, and if your program should happen to
> > crash right then, the array containing the data will disappear into
> > the ether and your file will be empty--in other words all your data
> > will be gone.  To prevent such an occurence, you should write your
> > final data to another file, then delete the original file, and finally
> > change the other file's name to the original file name.
>
> Some options:
>
> 1.  Limit line length.  Then offset is ( line* length ).
> 2.  Name your relation.
> 3.  Hijack the operating system.  Remember:
>
> abc
> def
>
> on disk looks like
> abc\ndef
>
> where
>
> abcd
> def
>
> looks like
> abcd\ndef
>
> So, if you want line 2, you have to scan line 1.- Hide quoted text -
>
> - Show quoted text -

You also have

f.seek( X )
f.write( 'abc' )

which writes 'abc' in the middle, at offset X.

f.seek( 0, SEEK_END )

takes you to the back, and don't forget

f.flush( ).

Say you have:

File:

abc
def
ghi

Symbols:

0-3
1-3
2-3

abcdefghi

and you want to make abc abC.  Then

f.seek( 2 )
f.write( 'C' )

does work.

If you want to make abc ab, then you change a different file

table.seek( 0 )
table.write( chr( 2 ) )

( or bytes( [ 2 ] ) in 3.0 ).

If you want to make abc abcd, then you change both.

f.seek( 9 )
f.write( 'd' )

table.seek( 0 )
table.write( chr( 4 ) )

But.  Now you have item 0 in two places.  'abc' somewhere and 'd'
somewhere else, at 0 and at 9 respectively.  I think the file system
overallocates room for String 1.  You would read:

0- 2- [ 3, 1 ]

for 'abc'+'d'

and

1- 1- [ 3 ]
2- 1- [ 3 ]

for 'def' and 'ghi', where actual representations would be:

0- 2- [ 0/3, 9/1, 0, 0, 0, 0, 0, 0 ]
1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]

As your chains start to grow, leave table 8xN, and just copy strings
to new sectors when they become long:

0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]

You can insort in linear time when you're looking for available slots.

'jklm' can't fit at 0.

0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
3- 1- [ 13/4, 0, 0, 0, 0, 0, 0, 0 ]

but 'nop' can.

0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
3- 1- [ 13/4, 0, 0, 0, 0, 0, 0, 0 ]
4- 1- [ 0/3, 0, 0, 0, 0, 0, 0, 0 ]

'jklm' could've with a split:

0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
3- 2- [ 0/3, 13/1, 0, 0, 0, 0, 0, 0 ]

and with 'nop':

0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
3- 2- [ 0/3, 13/1, 0, 0, 0, 0, 0, 0 ]
4- 1- [ 14/3, 0, 0, 0, 0, 0, 0, 0 ]

There's an easy chance you can beat DbfilenameShelf for your
application.



More information about the Python-list mailing list