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

后端 未结 5 1428
时光取名叫无心
时光取名叫无心 2020-11-27 05:02

I have a string like this:

s = u\"\"\"{\"desc\": \"\\u73cd\\u54c1\\u7f51-\\u5168\\u7403\\u6f6e\\u6d41\\u5962\\u54c1\\u7f51\\u7edc\\u96f6\\u552e\\u5546 
5条回答
  •  臣服心动
    2020-11-27 05:23

    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'))
    

提交回复
热议问题