help with lists and writing to file in correct order

homepricemaps at gmail.com homepricemaps at gmail.com
Thu Dec 29 19:42:44 EST 2005


hey mike-the sample code was very useful.  have 2 questions

when i use what you wrote which is listed below i get told
unboundlocalerror: local variable 'product' referenced before
assignment.  if i however chnage row to incident in "for incident in
bs('tr'):" i then get mytuples printed out nicely but once again get a
long list of

[('pizza;','pizza hut;', '3.94;')]
[('pizza;','pizza hut;', '3.94;')]


 for row in bs('tr'):
    	data=[]
    	for incident in row('h2',  {'id' : 'dealName'}):
        	productlist = []
		for oText in incident.fetchText( oRE):
            		productlist.append(oText.strip() + ';')
		product = ''.join(productlist)

	for incident in row('a',  {'name' : 'D0L3'}):
        	storelist = []
		for oText in incident.fetchText( oRE):
            		storelist.append(oText.strip() + ';')
		store = ''.join(storelist)

                 tuple = (product, store, price)
                 data.append(tuple)
                 print data




> 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