Python json.loads ValueError,需要分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28223242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:56:38  来源:igfitidea点击:

Python json.loads ValueError, expecting delimiter

pythonjsonpython-2.7

提问by AliBZ

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:

现在我需要使用 将它们加载到我的 python 代码中json.loads,但出现此错误:

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.loads,我明白了:

{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.loadsor something?

有没有办法在不添加额外内容的情况下做到这一点\?像传递参数之类的json.loads

回答by cdonts

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!

希望能帮助到你!

回答by Gandaro

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.

所以从\"to的转换"不是由 完成的json.loads,而是由 Python 本身完成的。

回答by curry_xyd

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

试试方法source.replace('""', '')还是分吧,因为""在源码中会使得json.loads(source)无法区分它们。

回答by Michael Vuolo

for my instance, i wrote:

就我而言,我写道:

STRING.replace("': '", '": "').replace("', '", '", "').replace("{'", '{"').replace("'}", '"}').replace("': \"", '": "').replace("', \"", '", "').replace("\", '", '", "').replace("'", '\"')

and works like a charm.

并且像魅力一样工作。