Snippets

Ilnur Ibragimov get_dict.py

Created by Ilnur Ibragimov
import re

x = {'foo[bar]': 1, 'foo[1]': '2'}


def get_dict(source, name):
    result = {}
    RE = re.compile(r'%s\[(\w+)\]' % name)
    print (source, source.keys())
    for k in source.keys():
        match_obj = RE.match(k)
        print (k, match_obj)
        if match_obj:
            result[match_obj.group(1)] = source[k]
    return result


print get_dict(x, 'foo')

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.