468,107 Members | 1,253 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

problems writing tuple to log file

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'):

data=[]

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

for incident in bs('span2'):
drinklist = []
for oText in incident.fetchText( oRE):
drinklist += oText.strip() + "','"
drink = ''.join(drinklist)
print drink


tuple = (food + drink "\n")
data.append(tuple)
f = open("data.txt", 'a')
f.write ( ''.join( tuple ) )

Feb 3 '06 #1
3 2458
lo************@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.
Feb 3 '06 #2
lo************@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 'o****@xiludom.gro'.split('@')])"
Feb 3 '06 #3
the, the issue is that the last loop adds the last value of everything
to the data array

Feb 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Pat Blair | last post: by
20 posts views Thread by Lucas Raab | last post: by
5 posts views Thread by eight02645999 | last post: by
15 posts views Thread by manstey | last post: by
2 posts views Thread by Michael Glassford | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.