how to find a lable quickly?

Miki miki.tebeka at gmail.com
Fri May 4 16:37:04 EDT 2007


Hello Frank,

> I am a new user on Python and I really love it.
The more you know, the deeper the love :)

> I have a big text file with each line like:
>
> label             3
> teststart       5
> endtest      100
> newrun     2345
>
> I opened the file by uu=open('test.txt','r') and then read the data as
> xx=uu.readlines()
This reads the whole file to memory, which might be a problem.

> In xx, it contains the list of each line. I want to find a spcefic labels
> and read the data. Currently, I
> do this by
> for ss in xx:
>    zz=ss.split( )
>   if zz[0] = endtest:
>     index=zz[1]
>
> Since the file is big and I need find more lables, this code runs slowly.
> Are there anyway to speed up the process? I thought to convert the data xx
> from list to a dictionay, so I can get the index quickly based on the
> label. Can I do that effeciently?
IMO a better way is either to not load the whole file to memory:
# Untested
labels = {}.fromkeys(["endtest", "other_label"])
for line in open("test.txt"):
    label, value = line.split()
    if label in labels:
        labels[label] = value.strip()

Another option is to use an external fast program (such as egrep):
from os import popen
labels = {}
for line in popen("egrep 'endtest|other_label' test.txt"):
    label, value = line.strip().split()
    labels[label] = value

HTH,
--
Miki <miki.tebeka at gmail.com>
http://pythonwise.blogspot.com/




More information about the Python-list mailing list