如果字符串有“\r”,Python 中的 json.loads(jsonstring) 会失败。即回车符
当我尝试时,我得到一个 JSON 字符串,该字符串在某处有一个 "\r"
字符,例如 "{"data":"foo \r\n bar"}"
解析它会抛出ValueError
。
>>> j="""{"data":"foo \r\n bar"}"""
>>> import json
>>> f=json.loads(j)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
f=json.loads(j)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 13 (char 13)
>>> j[13]
'\r'
"\r"
是 Python 字符串中完全合法的字符。
如何解析这个 JSON 字符串,以便
>>> dct = somehow_parse_json(j)
>>> dct['data']
'foo \r\n bar'
我可以轻松地找到并弹出回车符,但我希望它们可以被保存。
I am getting a JSON string which has a "\r"
character somewhere e.g. "{"data":"foo \r\n bar"}"
when I try to parse it throws ValueError
.
>>> j="""{"data":"foo \r\n bar"}"""
>>> import json
>>> f=json.loads(j)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
f=json.loads(j)
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 13 (char 13)
>>> j[13]
'\r'
"\r"
is a perfectly legal character in a Python string.
How can I parse this JSON string, such that
>>> dct = somehow_parse_json(j)
>>> dct['data']
'foo \r\n bar'
I could easily just find and pop carriage return characters, but I would prefer if they can be saved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在 JSON 中转义斜杠:
如果您没有转义它们,则您的 JSON 无效(是有效的 Python 字符串)。
You should escape slashes in JSON:
If you are not escaping them, your JSON is invalid (being valid Python string).
从逻辑上讲,
python
正在做应该做的事情!与旧的
CRLF
相同(灵感来自打字机)CR = 回车
LF = 换行
'\r' 代表
CR
但是 '\n' = CR + LF
所以,我的观点是,对于 json 来说,它绝对无效。
例如:
打印'\n 123456\rone'
# one3456
现在,如何使用
\r
?只能用
\\r
转义\r
Logically
python
is doing what should have been done !Its the same old
CRLF
(inspired from typewriters)CR = Carraige Return
LF = Line Feed
'\r' stands for
CR
But '\n' = CR + LF
so, my point is that for
json
its definitely not valid.For Eg:
print '\n 123456\rone'
# one3456
Now, how to use
\r
anyway ?That should only escape
\r
with\\r