我有 unicode u"{'code1':1,'code2':1}"
我想要字典格式。
我想要 {'code1':1,'code2':1}
格式。
我试过 unicodedata.normalize('NFKD', my_data).encode('ascii','ignore')
但它返回字符串而不是字典。
谁能帮帮我?
最佳答案
你可以使用内置的ast
包:
import ast
d = ast.literal_eval("{'code1':1,'code2':1}")
关于 ast 模块中的函数 literal_eval 的帮助:
literal_eval(node_or_string)
Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.
关于python - 将unicode字符串字典转换为python中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14950260/