The Little Guy Posted June 27, 2009 Share Posted June 27, 2009 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 More sharing options...
corbin Posted June 27, 2009 Share Posted June 27, 2009 If no match is found, a Match object is not returned. It would appear that no Match object is being returned since group is not a method. Link to comment https://forums.phpfreaks.com/topic/163840-solved-python-loop-through-dictionary/#findComment-864571 Share on other sites More sharing options...
The Little Guy Posted June 27, 2009 Author Share Posted June 27, 2009 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 https://forums.phpfreaks.com/topic/163840-solved-python-loop-through-dictionary/#findComment-864779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.