如何在python中将unicode转换为字典

时间:2017-08-17 15:43:11

标签: python dictionary unicode

我知道此前已经提出过这样的问题,但我无法找到解决问题的方法

我的功能是以这种格式返回unicode

{"set_id": "Rome", "group": "human", "attachments": null, , "object_type": "Prop", "attached": null, "tag_id": "prop", "Double Click": "reference", "files": "/makeup/Data/weapons/rifle", "source": "cupid", "attachment_label": null, "require_attachment": true, "rate": "high", "location": "/set2/ep1/Objects/weapons/machineguns/gun.txt", "attachment_type": null, "icon_path": "/resource/asset_manager/icons/rifle.bmp"}

在使用类型进行检查时,它显示为<type 'unicode'>

我试过ast.literal_eval(myver)但没有运气 哪些是将它转换为python字典的其他方法?

2 个答案:

答案 0 :(得分:1)

这看起来像JSON。您可能希望使用python的内置dict模块将其转换为python json

import json    
data = json.loads(myver)

答案 1 :(得分:-1)

json.loads不采用Unicode作为输入。

先尝试编码(将其转换为字符串),然后再尝试ast.literal_eval

喜欢:

newDict = ast.literal_eval(myver.encode("utf-8"))