Reputation: 51
I'm a python/coding newbie and I'm trying to put a two for loops into a while loop? Can I do this? How can I print out the dictionary mydict to make sure I am doing this correctly?
I'm stuck.
40 minutes later. Not stuck anymore. Thanks everyone!
def runloop():
while uid<uidend:
for row in soup.findAll('h1'):
try:
name = row.findAll(text = True)
name = ''.join(name)
name = name.encode('ascii','ignore')
name = name.strip()
mydict['Name'] = name
except Exception:
continue
for row in soup.findAll('div', {'class':'profile-row clearfix'}):
try:
field = row.find('div', {'class':'profile-row-header'}).findAll$
field = ''.join(field)
field = field.encode('ascii','ignore')
field = field.strip()
except Exception:
continue
try:
value = row.find('div', {'class':'profile-information'}).findAl$
value = ''.join(value)
value = value.encode('ascii','ignore')
value = value.strip()
return mydict
mydict[field] = value
print mydict
except Exception:
continue
uid = uid + 1
runloop()
Upvotes: 1
Views: 471
Reputation: 304167
You are not helping yourself by having these all over the place
except Exception:
continue
That basically says, "if anything goes wrong, carry one and don't tell me about it."
Something like this lets you at least see the exception
except Exception as e:
print e
continue
Is mydict
declared somewhere? That could be your problem
Upvotes: 1
Reputation: 2769
On nested loops:
You can nest for and while loops very deeply before python will give you an error, but it's usually bad form to go more than 4 deep. Make another function if you find yourself needing to do a lot of nesting. Your use is fine though.
Some problems with the code:
runloop()
to actually use the function.Upvotes: 0
Reputation: 837
You can put as many loops within other loops as you'd like. These are called nested loops.
Also, printing a dictionary is simple:
mydict = {}
print mydict
Upvotes: 1