Fast way to grab line from file?

Jimmie Houchin jhouchin at texoma.net
Thu Feb 24 10:29:15 EST 2000


Hello,

I don't know if you've solved this problem yet, but here goes with
what I just recently did.

I downloaded the last six month's of Zope's email archives to my PC at
work. However Netscape munges the imports and creates many bad
messages. The problem is that Netscape splits the file into messages
according to a 'from' on a newline. Legitimate froms on newlines which
were not headers became interpreted as such.

I created a script which parsed my directory of downloads read each
file and simply inserted a space before each non-header 'from'. This
required knowing which 'from's were headers and which were not. To do
this I needed to navigate within each file to determine if the 'from'
was followed by other headers. After working thru this I simpled
created a list after readlines.

f = open(file, 'r')
flist = []
for line in f.readlines():
    flist.append(line)

# insert other work here...

This allowed me to open, read, modify and write 28 files and 25
megabytes of messages in less than 90 seconds clock time on a PII 266
running Win95.

Writing your readlines() to a list can give line 100 by indexing the
list.

You could also do something like:  # warning not tested.

import NewBuiltins   
# NewBuiltins from Marc-André Lemburg
#http://starship.python.net/crew/lemburg/mxTools.html

f = open(file,'r')
for i, line in irange(f.readlines()):
    if i == 100:
        dowork(line)
    else: pass

Hope this can help.

Jimmie Houchin

On Tue, 22 Feb 2000 01:47:41 GMT, chiefteo69 at my-deja.com wrote:

>Hi there,
>  Does anyone know of a faster way to grab line
>100 from a given file without doing 100 readline()
>calls? This way is too slow, but I can't find out
>how to do this in python. Thanks,
>
>-Mateo
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.




More information about the Python-list mailing list