Last active
August 29, 2015 14:22
-
-
Save jimhorng/e8336178ad6acbe36d76 to your computer and use it in GitHub Desktop.
dot format to dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gen_dict(key_tree, value): | |
if len(key_tree) == 1: | |
return "%s = \"%s\";" %(key_tree[0], value) | |
else: | |
return (gen_dict(key_tree[1:], value) + | |
"\n" + "{key1} = {{}};\n{key1}['{key2}'] = {key2};".format(key1=key_tree[0], | |
key2=key_tree[1])) | |
print gen_dict(key_tree="a.b.c.d".split("."), value="haha") | |
# input: a.b.c.d = "haha" | |
# output: | |
# d = "haha"; | |
# c = {}; | |
# c['d']=d; | |
# b = {}; | |
# b['c']=c; | |
# a = {}; | |
# a['b']=b; | |
# or a:{ b:{ c:{ d: "haha" } } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment