开发者

More than one for-loop in a python while-loop

开发者 https://www.devze.com 2023-03-19 11:44 出处:网络
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 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:
                na开发者_高级运维me = 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()


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:

  • It will never reach the print statements because under the first for loop you have a return statement. When python sees a return inside a function, it will leave the function and present the return value.
  • I would avoid using try and except until you understand why you're getting the errors that you get without those.
  • Make sure the indentation is consistent. Maybe it's a copy and paste error, but it looks like the indentation of some lines is a character more than others. Make sure every tab is 4 spaces. Python, unlike most languages, will freak out if the indentation is off.
  • Not sure if you just didn't post the function call, but you would need to call runloop() to actually use the function.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消