Python json。加载ValueError,期待分隔符

时间:2021-11-07 06:26:24

I am extracting a postgres table as json. The output file contains lines like:

我将一个postgres表提取为json。输出文件包含如下行:

{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}

Now I need to load them in my python code using json.loads, but I get this error:

现在我需要使用json在python代码中加载它们。负载,但我得到这个错误:

Traceback (most recent call last):
  File "test.py", line 33, in <module>
    print json.loads('''{"id": 4, "data": {"test": 1, "hello": "I have \" !"}}''')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 50 (char 49)

I figured out the fix is to add another \ to \". So, if I pass

我发现解决方案是增加一个\到\”。所以,如果我通过

{"data": {"test": 1, "hello": "I have \\" !"}, "id": 4}

to json.loads, I get this:

json。加载,得到:

{u'data': {u'test': 1, u'hello': u'I have " !'}, u'id': 4}

Is there a way to do this without adding the extra \? Like passing a parameter to json.loads or something?

有没有一种方法可以不添加额外的\?比如将参数传递给json。加载还是什么?

3 个解决方案

#1


16  

You can specify so called “raw strings”:

您可以指定所谓的“原始字符串”:

>>> print r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}'
{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}

They don’t interpret the backslashes.

他们不理解反斜杠。

Usual strings change \" to ", so you can have " characters in strings that are themselves limited by double quotes:

通常的字符串会把“\”改为“”,这样你就可以在字符串中使用“字符”,而这些字符本身会受到双引号的限制:

>>> "foo\"bar"
'foo"bar'

So the transformation from \" to " is not done by json.loads, but by Python itself.

因此,从“\”到“”的转换不是由json完成的。装入,但由Python本身。

#2


4  

Try this:

试试这个:

json.loads(r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}')

If you have that string inside a variable, then just:

如果变量中有这个字符串,那么:

json.loads(data.replace("\\", r"\\"))

Hope it helps!

希望它可以帮助!

#3


0  

Try the ways source.replace('""', '') or sub it, cause "" in the source will make json.loads(source) can not distinguish them.

试的方法来源。替换(' '' ','')或下标it,因为在源中会使json.load(源)无法区分它们。

#1


16  

You can specify so called “raw strings”:

您可以指定所谓的“原始字符串”:

>>> print r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}'
{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}

They don’t interpret the backslashes.

他们不理解反斜杠。

Usual strings change \" to ", so you can have " characters in strings that are themselves limited by double quotes:

通常的字符串会把“\”改为“”,这样你就可以在字符串中使用“字符”,而这些字符本身会受到双引号的限制:

>>> "foo\"bar"
'foo"bar'

So the transformation from \" to " is not done by json.loads, but by Python itself.

因此,从“\”到“”的转换不是由json完成的。装入,但由Python本身。

#2


4  

Try this:

试试这个:

json.loads(r'{"data": {"test": 1, "hello": "I have \" !"}, "id": 4}')

If you have that string inside a variable, then just:

如果变量中有这个字符串,那么:

json.loads(data.replace("\\", r"\\"))

Hope it helps!

希望它可以帮助!

#3


0  

Try the ways source.replace('""', '') or sub it, cause "" in the source will make json.loads(source) can not distinguish them.

试的方法来源。替换(' '' ','')或下标it,因为在源中会使json.load(源)无法区分它们。