python - Loading unicode json/serialized dictionary fails due to unicode error
问题描述
I'm given some input that I must parse and convert to a Dict
. I don't control how the input is generated.
An example input is u'{u\'my_key\': u\'AB\\N\'}'
. Notice the this should represent a serialized dictionary.
Parsing this dictionary string fails using a variety of methods. Using json.loads
fails due to the structure of the string being malformed due to the nested u
. Using ast.literal_eval
fails with a (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: malformed \N character escape
error.
I need to somehow sanitize the input so the \N
won't be considered an ascii
character when parsed with ast
. Doing a simple replace('\\', '\\\\')
seems error prone and probably has many edge cases.
Alternatively, I need a way to remove the u
from the nested string so json.loads
would work.
Thanks
解决方案
处理这种输入并不容易。事实上,我能找到的唯一解决方案是这个:
input_data = u'{u\'my_key\': u\'AB\\N\'}'
i = input_data\
.replace('\'', '"')\
.replace('u', '')\
.replace('\\', '\\\\')
data = json.loads(i)
print(type(data))
# <type 'dict'>
它可能会解决您的具体示例,但我不鼓励在您的项目中使用它。
正如@snakecharmerb 所说,我还建议对输入执行某种策略并在发送之前验证json字符串,例如使用类似的东西。
推荐阅读
- javascript - jquery-ui ui-draggable 不拖动
- python - Pandas 通过合并两个数据框和匹配行来创建新列
- angular-cli - ng 构建问题:找不到插件“proposal-class-properties”
- javascript - 日历日期显示提前一天
- java - 缺少 Esclipse IDE java.awt 和 java.swing 建议?
- excel - 用于在不同行上进行多条件查找的 Excel 函数
- javascript - 当 app 是 Promise 时启动 mocha chai 测试
- python - 如何从python中的字典创建元组列表?
- python - 在 Tensorflow 中将 1D 张量与 4D 张量相乘
- c# - 如何使用带有 C# 和 .NET 的 MultipartReader 下载流式图像?