Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python json.loads fails with `ValueError: Invalid control character at: line 1 column 33 (char 33)`

Tags:

python

json

I have a string like this:

s = u"""{"desc": "\u73cd\u54c1\u7f51-\u5168\u7403\u6f6e\u6d41\u5962\u54c1\u7f51\u7edc\u96f6\u552e\u5546 <br \/>\r\nhttp:\/\/www.zhenpin.com\/ <br \/>\r\n<br \/>\r\n200\u591a\u4e2a\u56fd\u9645\u4e00\u7ebf\u54c1\u724c\uff0c\u9876\u7ea7\u4e70\u624b\u5168\u7403\u91c7\u8d2d\uff0c100%\u6b63\u54c1\u4fdd\u969c\uff0c7\u5929\u65e0\u6761\u2026"}""" 

json.loads(s) returns error message like this:

ValueError: Invalid control character at: line 1 column 33 (char 33) 

Why does this error occur? How can I solve this problem?

like image 280
福气鱼 Avatar asked Feb 15 '12 14:02

福气鱼


2 Answers

Another option, perhaps, is to use the strict=False argument

According to http://docs.python.org/2/library/json.html

"If strict is False (True is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0-31 range, including '\t' (tab), '\n', '\r' and '\0'."

For example:

json.loads(json_str, strict=False) 
like image 129
kejbaly2 Avatar answered Sep 23 '22 13:09

kejbaly2


The problem is your unicode string contains carriage returns (\r) and newlines (\n) within a string literal in the JSON data. If they were meant to be part of the string itself, they should be escaped appropriately. If they weren't meant to be part of the string, they shouldn't be in your JSON either.

If you can't fix where you got this JSON string to produce valid JSON, you could either remove the offending characters:

>>> json.loads(s.replace('\r\n', '')) 

or escape them manually:

>>> json.loads(s.replace('\r\n', '\\r\\n')) 
like image 24
Thomas Wouters Avatar answered Sep 21 '22 13:09

Thomas Wouters