Jump to content

[SOLVED] Python loop through dictionary


Recommended Posts

Why does this work:

m = re.search("(create database) (.*)", cmd)
print m.group(2)

 

And this doesn't work:

commands = {"(create database) (.*)": "create database", "(drop database) (.*)": "drop database"}
        for k, v in commands.iteritems():
                key = str(k)
                m = re.search(key, cmd)
                print m.group(2)

 

I get this error:

 

    print m.group(2)

AttributeError: 'NoneType' object has no attribute 'group'

 

 

What is supposed to happen is that the loop will loop through my dictionary, and do a regular expression on each item until it finds the correct item, but it cant do that for some reason, any one have any ideas?

Link to comment
Share on other sites

OK, so what I did was put it inside a try catch block, and it works, so it now looks like this:

 

            try:
                if m.group(1) == key:
                    mainCmd = key
                    value = m.group(2)
                    break
            except:
                continue

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.