473,473 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to find a lable quickly?

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

__________________________________________________ _______________
$B%a%C%;%s%8%c!<$*M'C#>R2p%W%l%<%s%HBh(B2$BCF3+ ;O!*%i%9%Y%,%9N99T%W%l%<%s%H(B
http://campaign.live.jp/dizon/

May 4 '07 #1
3 1215
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

__________________________________________________ _______________
$B%a%C%;%s%8%c!<$*M'C#>R2p%W%l%<%s%HBh(B2$BCF3+ ;O!*%i%9%Y%,%9N99T%W%l%<%s%H(B
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
May 4 '07 #2
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 <mi*********@gmail.com>
http://pythonwise.blogspot.com/

May 4 '07 #3
"wang frank" <fw*@hotmail.co.jpwrote:
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.
May 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lukelrc | last post by:
Hi, I have a listfield that displays a list of 'Themes' stored in a database. The listfield's lable is Themenames and it's value is UniqueThemeID. The page that i'm designing allows the user to...
8
by: pigeon | last post by:
I have 2 users that their client software must be going crazy.. they are sending packets every .02 seconds to the db server... I know this because I stuck a sniffer on teh traffic.. but now i just...
3
by: JD | last post by:
Is it possible to have a clear backstyle for a lable control? I want to write some text on a lable and I want the background of the label to match the color of the control I am placing the label...
1
by: mainecats | last post by:
I am trying to resize a lable using VB 2005. I go to the Properties window and choose size. I put the numbers in and when I click on anything else the numbers go back to the default size. What am I...
1
by: Prashwee | last post by:
Hi All Just a small problem. I am using DataGridView control for my project implementing in VB 2005. I need to resize my column header lable withd dyamically so that the user can see the lable...
0
by: viral123 | last post by:
Hi I am using asp.net application on server side. I have two web forms as page1.aspx and page2.aspx how can i make change in page2.aspx Lable by clicking on page1.aspx form button. I really...
0
by: Ryan Liu | last post by:
Hi, In VS 2005, in design view, if I put a lable on a windows form, and set label Text to "", then this label will totally invisiable. This is right at runtime, but I remember in VS 2003, it...
2
by: ravindarjobs | last post by:
Hi friends i want to generate UPS label from my asp.net C# code. i have UPS account, online tools information with me. but dont know where to start to develop. can any one plz give a link that...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.