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
https://forums.phpfreaks.com/topic/163840-solved-python-loop-through-dictionary/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.