在Python中解析无效的Unicode JSON [英] Parsing invalid Unicode JSON in Python

查看:373
本文介绍了在Python中解析无效的Unicode JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有问题的json字符串,其中包含一些时髦的unicode字符

i have a problematic json string contains some funky unicode characters

"test":{"foo":"Ig0s\x5C/k\x5C/4jRk"}}

如果我使用python进行转换

and if I convert using python

import json
s = r'{"test":{"foo":"Ig0s\x5C/k\x5C/4jRk"}}'
json.loads(s) 
# Error..

如果我可以跳过/丢失这些Unicode字符的值,那么使我的json.loads(s)正常工作的最佳方法是什么?

If I can accept to skip/lose the value of these unicode characters, what is the best way to make my json.loads(s) works?

推荐答案

您没有JSON;可以直接解释为Python.使用 ast.literal_eval() :

You don't have JSON; that can be interpreted directly as Python instead. Use ast.literal_eval():

>>> import ast
>>> s = r'{"test":{"foo":"Ig0s\x5C/k\x5C/4jRk"}}'
>>> ast.literal_eval(s)
{'test': {'foo': 'Ig0s\\/k\\/4jRk'}}

\x5C是一个单反斜杠,在这里的Python文字字符串表示形式中已加倍.实际的字符串值为:

The \x5C is a single backslash, doubled in the Python literal string representation here. The actual string value is:

>>> print _['test']['foo']
Ig0s\/k\/4jRk

这会将输入解析为Python源,但仅允许使用文字值;字符串,NoneTrueFalse,数字和容器(列表,元组,字典).

This parses the input as Python source, but only allows for literal values; strings, None, True, False, numbers and containers (lists, tuples, dictionaries).

此方法比json.loads()慢,因为它在纯Python代码中执行了部分解析树处理.

This method is slower than json.loads() because it does part of the parse-tree processing in pure Python code.

另一种方法是使用正则表达式用JSON \uhhhh代码替换\xhh转义代码:

Another approach would be to use a regular expression to replace the \xhh escape codes with JSON \uhhhh codes:

import re

escape_sequence = re.compile(r'\\x([a-fA-F0-9]{2})')

def repair(string):
    return escape_sequence.sub(r'\\u00\1', string)

演示:

>>> import json
>>> json.loads(repair(s))
{u'test': {u'foo': u'Ig0s\\/k\\/4jRk'}}

如果您可以修复产生此值的 source 来输出实际的JSON,那将是一个很多更好的解决方案.

If you can repair the source producing this value to output actual JSON instead that'd be a much better solution.

这篇关于在Python中解析无效的Unicode JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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