Python中JSON加载中的单引号与双引号
我注意到单引号导致simplejson
的loads
函数失败:
I notice that single quotes cause simplejson
's loads
function to fail:
>>> import simplejson as json
>>> json.loads("\"foo\"")
'foo'
>>> json.loads("\'foo\'")
Traceback (most recent call last):
...
ValueError: No JSON object could be decoded
我正在将类似以下内容的内容解析:foo = ["a", "b", "c"]
从文本文件转换为Python中的列表,并且还希望接受foo = ['a', 'b', 'c']
. simplejson
便于将foo
自动添加到列表中.
I'm parsing things like: foo = ["a", "b", "c"]
from a textfile into lists in Python and would like to also accept foo = ['a', 'b', 'c']
. simplejson
is convenient for making foo
automatically into a list.
如何使loads
接受单引号,或自动将双引号替换为单引号而不破坏输入?谢谢.
How can I get loads
to accept single quotes, or automatically substitute double for single quotes without wrecking the input? thanks.
使用正确的工具完成工作,您不是在解析JSON,而是在解析Python,因此请使用
Use the proper tool for the job, you are not parsing JSON but Python, so use ast.literal_eval()
instead:
>>> import ast
>>> ast.literal_eval('["a", "b", "c"]')
['a', 'b', 'c']
>>> ast.literal_eval("['a', 'b', 'c']")
['a', 'b', 'c']
>>> ast.literal_eval('["mixed", \'quoting\', """styles"""]')
['mixed', 'quoting', 'styles']
-
JSON文档始终对字符串使用双引号,对于
\uhhhh
十六进制转义语法使用UTF-16,对于键-值对具有{...}
对象,其中键始终为字符串,而序列始终为[...]
列表,并使用null
,true
和false
值;注意小写的布尔值.数字以整数和浮点数形式出现.-
JSON documents always use double quotes for strings, use UTF-16 for
\uhhhh
hex escape syntax, have{...}
objects for key-value pairs with keys always strings and sequences are always[...]
lists, and usenull
,true
andfalse
values; note the lowercase booleans. Numbers come in integer and floating point forms.在Python中,字符串表示形式可以使用单引号和双引号,Unicode转义使用
\uhhhh
和\Uhhhhhhhh
形式(没有UTF-16代理对),具有{...}
显示语法的词典可以在许多键中使用键序列可以是列表([...]
),但也可以使用元组((...)
),也可以是其他类型的容器,而不仅仅是字符串. Python具有None
,True
和False
(标题框!),数字以整数,浮点数和复杂形式出现.In Python, string representations can use single and double quotes, Unicode escapes use
\uhhhh
and\Uhhhhhhhh
forms (no UTF-16 surrogate pairs), dictionaries with{...}
display syntax can have keys in many different types rather than just strings, sequences can be lists ([...]
) but can also use tuples ((...)
), or you could have other container types still. Python hasNone
,True
andFalse
(Titlecase!) and numbers come in integers, floats, and complex forms.当解码碰巧成功但数据被错误地解释时(例如使用逃脱的非BMP代码点,例如Emoji),将彼此混淆会导致解析错误或细微问题.确保使用正确的方法对其进行解码!在大多数情况下,当您确实拥有Python语法数据时,某人实际上使用了错误的编码方法,并且仅偶然生成了Python表示形式.看看在这种情况下源是否需要修复;通常,输出是通过使用
str(object)
产生的,其中应使用json.dumps(obj)
代替.Confusing one with the other can either lead to parse errors or subtle problems when decoding happened to succeed but the data has been wrongly interpreted, such as with escaped non-BMP codepoints such Emoji. Make sure to use the right method to decode them! And in most cases when you do have Python syntax data someone actually used the wrong method of encoding and only accidentally produced Python representations. See if the source needs fixing in that case; usually the output was produced by using
str(object)
wherejson.dumps(obj)
should have been used instead.
-