python - 将unicode字符串字典转换为python中的字典

标签 python unicode dictionary

我有 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/

相关文章:

python - tensorflow:LSTM 单元中变量的初始化程序

c++ - SDL 2.0 TextInputEvent UTF8数据存储

java - 方法 getBytes() 返回未知字节

python - 使用 boto3 通过与特定文件名匹配的 S3 对象分页

python - Tkinter 和绑定(bind)到 ListboxSelect 事件的两个列表框出现意外行为

python - 嵌套字典Python中的计数器

c++ - 向 typedef 添加函数

c++ - 为什么在声明 std::set 时需要重复排序子例程?

python - 如何在 sklearn 中编写自定义估算器并对其使用交叉验证?

javascript - i18next 翻译显示为 unicode 十六进制字符