problems writing tuple to log file

bruno at modulix onurb at xiludom.gro
Fri Feb 3 12:11:38 EST 2006


localpricemaps at gmail.com wrote:
> i am having a problem writing a tuple to a text file.  my code is
> below.

I'd rather say you are having a problem with logic.

> what i end up getting is a text file that looks like this
> 
> burger, 7up
> burger, 7up
> burger, 7up

Which is exactly what one would expect (in the best case...) given the
code you've written.

> and this is instead of getting a list that should look like this
> 
> burger, 7up
> fries ,coke
> cake ,milk
> 
> 
> however there is something faulty in my writing of the tuple to the
> text file. 

Nope, the problem is elsewhere.

> perhaps related to the indentation that causes it to write
> the same stuff over and over?

You clearly have a problem with indentation (hint : use spaces not
tabs), but this is not the cause of your problem.

> 
> 
>     for row in bs('div'):
> 
> 	data=[]
> 
> 	for incident in bs('span'):
>         	foodlist = []

Do you understand that this reinitialise foodlist on each iteration ?

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

Concatening a string to a list may not exactly do what you think. Try
printing foodlist, you'll be surprised.

> 		food = ''.join(foodlist)

Do you understand that this overwrite 'food' on each iteration ?

> 		print food
> 

> 
> 	for incident in bs('span2'):
>         	drinklist = []

Same observation as above

> 		for oText in incident.fetchText( oRE):
>             		drinklist += oText.strip() + "','"

idem

> 		drink = ''.join(drinklist)

idem

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

1/ dont use 'tuple' as an identified, it shadows the builtin type tuple.
2/ anyway, this is *not* a tuple. What you get here is a string made of
the concatenation of the actual values of food and drink plus a newline.

Ok, at this stage, the name 'food'  *may* exist, in which case it'll be
bound to the value found for the last iteration of the first 'for' loop.
Note that it may also not exist at all, if bs('span') returns an empty
sequence - in which case you'll get a nice NameError exception. Same for
'drink' of course.


>         data.append(tuple)

Why to you append this to a list that you don't use ?

>       f = open("data.txt", 'a')

This may fail. Please use a try/except block.

>       f.write ( ''.join( tuple ) )


And please close the file once done.


Your code is such a mess that it's difficult to know for sure what
you're trying to do - and you don't provide much context (hint : when
asking for help, try and post the minimal *runnable* code that exhibit
your problem - 'runnable' meaning that anyone can run your snippet in
it's python interpreter).

What follows is an attempt at rewriting the whole damn thing so it as at
least a chance to behave sensibly - I wouldn't bet that this actually
what you *should* write but I hope this may help you understand where
your errors are. But please dont ask for further help on this code
before you've folowed a good (preferably programming-newbie oriented)
Python tutorial ('learning to think like a computer scientist' may be a
wise choice).

# -----
data=[]
for row in bs('div'):
  foodlist = []
  for incident in bs('span'):
    b = incident.findPrevious('b')
    for oText in b.fetchText( oRE):
      foodlist.append(oText.strip())

  drinklist = []
  for incident in bs('span2'):
    for oText in incident.fetchText( oRE):
      drinklist.append(oText.strip())
  # I suppose you expect to have 1 drink for 1 food
  assert len(foodlist) == len(drinklist)
  pairs = zip(foodlist, drinklist)
  data += pairs

try:
  f = open("data.txt", 'a')
except IOError, e:
  # handle error here
  print "oops, failed to open 'data.txt' : %s" % e
else:
  f.write("\n".join(["%s, %s" % pair for pair in data])
  f.close()

# -----


A last advice : Python comes with an interactive interpreter, which is a
real powertool for learning, testing and debugging. So *use it*.

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list