problems writing tuple to log file

L

localpricemaps

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 ) )
 
J

Juho Schultz

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.
 
B

bruno at modulix

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
 
L

localpricemaps

the, the issue is that the last loop adds the last value of everything
to the data array
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top