help with lists and writing to file in correct order

Mike Meyer mwm at mired.org
Thu Dec 29 16:08:54 EST 2005


homepricemaps at gmail.com writes:
> hey kent thanks for your help.
>
> so i ended up using a loop but find that i end up getting the same set
> of results every time.  the code is here:
>
>     for incident in bs('tr'):
> 	data2 = []
>     	for incident in bs('h2',  {'id' : 'dealName'}):
>         	product2 = ""
> 		for oText in incident.fetchText( oRE):
>             		product2 += oText.strip() + ';'
>
>
>
>       	for incident in bs('a',  {'name' : 'D0L3'}):
>         	store2 = ""
> 		for oText in incident.fetchText( oRE):
>             		store2 += oText.strip() + ';'
>
>
>     	for incident in bs('a',  {'class' : 'nojs'}):
>         	price2 = ""
> 		for oText in incident.fetchText( oRE):
>             		price2 += oText.strip() + ';'
>
>
> 	tuple2 = (product2, store2, price2)
> 	data2.append(tuple2)
> 	print data2

Two things here that are bad in general:
1) Doing string catenations to build strings. This is slow in
   Python. Build lists of strings and join them, as below.

2) Using incident as the index variable for all four loops. This is
   very confusing, and certainly part of your problem.

> and i end up getting the following instead of unique results
>
> pizza, pizzahut, 3.94
> pizza, pizzahut, 3.94
> pizza, pizzahut, 3.94
> pizza, pizzahut, 3.94

Right. The outer loop doesn't do anything to change what the inner
loops search, so they do the same thing every time through the outer
loop. You want them to search the row returned by the outer loop each
time.

for row in bs('tr'):
    data2 = []
    for incident in row('h2', {'id' :'dealName'}):
        product2list = []
        for oText in incident.fetchText(oRE):
            product2list.append(OText.strip() + ';')
        product2 = ''.join(product2list)
        # etc.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list