将unicode字典字典转换为python中的字典 [英] Convert unicode string dictionary into dictionary in python

查看:596
本文介绍了将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:


literal_eval(node_or_string)



安全评估表达式节点或包含Python表达式的字符串。所提供的字符串或节点可能只包含以下Python文字结构:字符串,数字,元组,列表,dicts,booleans和无。



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:

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.

这篇关于将unicode字典字典转换为python中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆