problems writing tuple to log file

Juho Schultz juho.schultz at helsinki.fi
Fri Feb 3 11:48:54 EST 2006


localpricemaps at gmail.com wrote:
> i am having a problem writing a tuple to a text file.  my code is
> below.
> 
> what i end up getting is a text file that looks like this
> 
> burger, 7up
> burger, 7up
> burger, 7up
> 
> and this is instead of getting a list that should look like this
> 
> burger, 7up
> fries ,coke
> cake ,milk
> 
> note that i have print statements that print out the results of the
> scraping and they are fine.  they print out burger, fries, cake and
> then 7up, coke, milk
> 
> however there is something faulty in my writing of the tuple to the
> text file.  perhaps related to the indentation that causes it to write
> the same stuff over and over?
> 
> 
> 
>     for row in bs('div'):

What kind of function is 'bs'? Should you use 'row'
(which you are looping over) inside the loop?
Seems that your code is equal to

for row in range(len(bs('div'))):

> 	for incident in bs('span'):

Just like you use 'incident' here, inside the other loop.


>         	foodlist = []
> 		b = incident.findPrevious('b')
> 		for oText in b.fetchText( oRE):
>             		#foodlist.append(oText.strip() + "',")
> 				foodlist += oText.strip() + "','"
> 		food = ''.join(foodlist)
> 		print food
> 

After "print food" you repeat the loop, overwriting "food" until last 
round. And after you have found the last "food", you put it in "tuple".

>         tuple = (food + drink "\n")

A tip: 'tuple' is a built-in function, just like 'open' you use.
This statement overwrites that function with a string.
It is usually a good idea to leave the built-ins as they are,
and use some other names for variables.



More information about the Python-list mailing list