how to find a lable quickly?

Duncan Booth duncan.booth at invalid.invalid
Fri May 4 16:46:45 EDT 2007


"wang frank" <fw3 at hotmail.co.jp> wrote:

> Hi,
> 
> I am a new user on Python and I really love it. 
> 
> 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()

First suggestion: never use readlines() unless you really want all the 
lines in a list. Iterating over the file will probably be faster 
(especially if some of the time you can abort the search without reading 
all the way to the end).

> 
> 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]

Ignoring the fact that what you wrote wouldn't compile, you could try:

   if ss.startwith('endtest '):
      ...

> 
> 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?
> 
Yes, if you need to do this more than once you want to avoid scanning the 
file repeatedly. So long as you are confident that every line in the file 
is exactly two fields:

   lookuptable = dict(s.split() for s in uu)

is about as efficient as you are going to get.



More information about the Python-list mailing list