Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

write to specific line in file?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


skanemupp at yahoo

May 16, 2008, 11:41 AM

Post #1 of 7 (252 views)
Permalink
write to specific line in file?

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?
--
http://mail.python.org/mailman/listinfo/python-list


iqbaltalaat at gmail

May 16, 2008, 11:53 AM

Post #2 of 7 (247 views)
Permalink
Re: write to specific line in file? [In reply to]

Open the file inside your script in append mode.

open('filename', 'wa')


On May 16, 11:41 pm, globalrev <skanem...@yahoo.se> 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?

--
http://mail.python.org/mailman/listinfo/python-list


bbxx789_05ss at yahoo

May 16, 2008, 12:25 PM

Post #3 of 7 (243 views)
Permalink
Re: write to specific line in file? [In reply to]

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.

--
http://mail.python.org/mailman/listinfo/python-list


castironpi at gmail

May 16, 2008, 3:22 PM

Post #4 of 7 (243 views)
Permalink
Re: write to specific line in file? [In reply to]

On May 16, 2:25 pm, 7stud <bbxx789_0...@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.
--
http://mail.python.org/mailman/listinfo/python-list


castironpi at gmail

May 16, 2008, 3:57 PM

Post #5 of 7 (243 views)
Permalink
Re: write to specific line in file? [In reply to]

On May 16, 5:22 pm, castironpi <castiro...@gmail.com> wrote:
> On May 16, 2:25 pm, 7stud <bbxx789_0...@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.
--
http://mail.python.org/mailman/listinfo/python-list


castironpi at gmail

May 17, 2008, 5:58 PM

Post #6 of 7 (229 views)
Permalink
Re: write to specific line in file? [In reply to]

On May 16, 5:57 pm, castironpi <castiro...@gmail.com> wrote:
> On May 16, 5:22 pm, castironpi <castiro...@gmail.com> wrote:
>
>
>
>
>
> > On May 16, 2:25 pm, 7stud <bbxx789_0...@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.- Hide quoted text -
>
> - Show quoted text -

I am still interested in this one, but doubt if anyone still reading.
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py2 at yahoo

May 17, 2008, 8:50 PM

Post #7 of 7 (228 views)
Permalink
Re: write to specific line in file? [In reply to]

En Sat, 17 May 2008 21:58:50 -0300, castironpi <castironpi[at]gmail.com> escribió:

> I am still interested in this one, but doubt if anyone still reading.

I've not filtered you out *yet* - but that may happen very soon.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.