字符串转字典
两种转换方法
str = "{a:1, b:3}" a = demjson.decode(str) print(a, type(a)) <<< {'a': 1, 'b': 3} <class 'dict'>
dic2 = eval(a) print(dic2, type(dic2))
<<< TypeError: eval() arg 1 must be a string, bytes or code object
把str的键加上转为字符串"{'a':1, 'b':3}"就可以正常运行
dic2 = eval("{'a':1, 'b':3}")
print(dic2, type(dic2))
<<< {'a': 1, 'b': 3} <class 'dict'>