我有一个简单的例子:
test = '{ "text": "\"test\""}'
Run Code Online (Sandbox Code Playgroud)
这是一个有效的json(参见http://jsonlint.com/).
但是simplejson.loads(test)返回错误:
ValueError: Expecting , delimiter: line 1 column 12 (char 12)
Run Code Online (Sandbox Code Playgroud)
为什么?
在Python,\"意味着"只.因此,字符串实际上被处理为
{ "text": ""test""}
Run Code Online (Sandbox Code Playgroud)
如您所见,之前有一个空字符串,test并且test不用双引号括起来.所以,你也必须\像这样逃避
test = '{ "text": "\\"test\\"" }'
Run Code Online (Sandbox Code Playgroud)
或者将文本创建为原始字符串,如下所示
test = r'{ "text": "\"test\"" }'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
124 次 |
| 最近记录: |