how to find a lable quickly?

Larry Bates larry.bates at websafe.com
Fri May 4 16:05:53 EDT 2007


wang frank 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()
> 
> 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?
> 
> Thanks
> 
> Frank
> 
> _________________________________________________________________
> メッセンジャーお友達紹介プレゼント第2弾開始!ラスベガス旅行プレゼント
> http://campaign.live.jp/dizon/

Are the labels unique?  That is, labels are never repeated in the file.  If
not you are going to need to do some processing because dictionary keys
must be unique.

Do you have control over the format of the test.txt file.  If so a small
change would put it into a format that the ConfigParser module can handle
which would make it faster because it uses dictionaries.


[labels]
label=3
teststart=5
endtest=100
newrun=2345

With this you can have different sections [section] with labels under each
section.  Use configParser to read this and then get options with
geting(section, option).

-Larry



More information about the Python-list mailing list