indentation messing up my tuple?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Jan 27 20:27:41 EST 2006


On Fri, 27 Jan 2006 16:45:54 -0800, localpricemaps wrote:

> here is my code:
> 
>     for row in bs('div',  {'style' : 'both'}):

What is bs? Is it a secret?


>     	data=[]
> 
>     	for incident in bs('h3',  {'class' : 'name'}):
>         	foodlist = []
> 		for oText in incident.fetchText( oRE):
>             		foodlist.append(oText.strip() + "','")
> 		food = ''.join(foodlist)

Put a "print food" statement at the end of this line to see what you have.

>     	for incident in bs('span',  {'class' : 'drink'}):
> 		drink = incident.findNextSibling('a', {'class': 'nojs'}) drinklist =
> 		[]
> 		for oText in drink.fetchText( oRE):
>             		drinklist.append(oText.strip() + "','")
> 		drink = ''.join(drinklist)

Put a "print drink" statement at the end of this line to see what you have.

>     	tuple = (food + drink + "\n")
>     	data.append(tuple)

This seems awfully pointless. At the beginning of every loop, you set data
to the empty list. After a while you append one tuple to it. But you don't
use data again, and it just gets reset to the empty list at the beginning
of the next loop.

What is the purpose of data?

>     	f = open("test.txt", 'a')
>     	f.write ( ''.join( tuple ) )

You are re-opening the file every single time around the loop. You should
either do this:

f = open("test.txt", "w")
for row in bs(...):
    # processing 
    f.write(one line only)
f.close()

or do this:

data = []
for row in bs(...):
    # processing 
    # accumulate everything you want in data
f.writelines(data)
f.close()


-- 
Steven.




More information about the Python-list mailing list