本文介绍了将unicode字典字典转换为python中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有unicode u{'code1':1,'code2':1}
,我想要它的字典格式。
我想在 {'code1':1,'code2':1}
格式。
我试过 unicodedata.normalize('NFKD',my_data).encode('ascii','ignore')
但它返回字符串不是字典。 p>
任何人都可以帮我吗?
解决方案
您可以使用内置的 ast
package:
import ast
d = ast.literal_eval({'code1':1,'code2':1})
帮助在模块中的函数literal_eval ast:
I have unicode u"{'code1':1,'code2':1}"
and I want it in dictionary format.
I want it in {'code1':1,'code2':1}
format.
I tried unicodedata.normalize('NFKD', my_data).encode('ascii','ignore')
but it returns string not dictionary.
Can anyone help me?
解决方案
You can use built-in ast
package:
import ast
d = ast.literal_eval("{'code1':1,'code2':1}")
Help on function literal_eval in module ast:
这篇关于将unicode字典字典转换为python中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!