将python JSON打印回python

2024-06-23 03:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,有人给我发送了一个JSON转储的数据,但他们显然是懒洋洋地(通过打印)在python中完成的,所以(简化的)数据是:

{u'x': u'somevalue', u'y': u'someothervalue'}

而不是有效的JSON:

{"x": "somevalue", "y": "someothervalue"}

因为它不是有效的JSON,json.loads文件()自然无法解析它。你知道吗

Python是否包含这样的模块来解析自己的输出?事实上,我认为自己解析它可能比试图向这个家伙解释他做错了什么以及如何修复它要快。你知道吗


Tags: 模块文件数据jsonloads家伙somevaluesomeothervalue
2条回答

您可能可以通过以下方式逃脱:

>>> s = "{u'x': u'somevalue', u'y': u'someothervalue'}"
>>> from ast import literal_eval
>>> literal_eval(s)
{u'y': u'someothervalue', u'x': u'somevalue'}

demjsonpython模块允许严格和非严格操作。以下是非严格模式下的一些津贴列表:

The following are permitted when processing in NON-STRICT mode:

* Unicode format control characters are allowed anywhere in the input.
* All Unicode line terminator characters are recognized.
* All Unicode white space characters are recognized.
* The 'undefined' keyword is recognized.
* Hexadecimal number literals are recognized (e.g., 0xA6, 0177).
* String literals may use either single or double quote marks.
* Strings may contain \x (hexadecimal) escape sequences, as well as the
  \v and \0 escape sequences.
* Lists may have omitted (elided) elements, e.g., [,,,,,], with
  missing elements interpreted as 'undefined' values.
* Object properties (dictionary keys) can be of any of the
  types: string literals, numbers, or identifiers (the later of
  which are treated as if they are string literals) -as permitted
  by ECMAScript.  JSON only permits strings literals as keys.

相关问题 更多 >

    热门问题