我正在尝试解析python中的unicode字符串并遇到Invalid \escape
错误。我见过这个post,但它也没有帮助。
这是我的代码:
def replace_with_byte(match):
return chr(int(match.group(0)[1:], 8))
def test(request):
params = RequestLog.objects.get(id = request_id).params
params = params[2:len(params)-1]
invalid_escape = re.compile(r'\\[0-7]{1,3}')
params = invalid_escape.sub(replace_with_byte, params)
params = json.loads(params) # The error rises here
# ...
这是params
的价值:
[{"display_name":"My calendar","owner_account":"My calendar","client_id":"1","name":"My calendar"},{"display_name":"navasardyan.edgar@gmail.com","owner_account":"navasardyan.edgar@gmail.com","client_id":"2","color":"15","name":"navasardyan.edgar@gmail.com"},{"display_name":"Contacts","owner_account":"#contacts@group.v.calendar.google.com","client_id":"3","color":"9","name":"Contacts"},{"display_name":"Holidays in Armenia","owner_account":"en.am#holiday@group.v.calendar.google.com","client_id":"4","color":"17","name":"Holidays in Armenia"},{"display_name":"\xd0\x9f\xd1\x80\xd0\xb0\xd0\xb7\xd0\xb4\xd0\xbd\xd0\xb8\xd0\xba\xd0\xb8 \xd0\xa0\xd0\xa4","owner_account":"ru.russian#holiday@group.v.calendar.google.com","client_id":"5","color":"12","name":"\xd0\x9f\xd1\x80\xd0\xb0\xd0\xb7\xd0\xb4\xd0\xbd\xd0\xb8\xd0\xba\xd0\xb8 \xd0\xa0\xd0\xa4"},{"display_name":"Test","owner_account":"0nptlmsb73cnqbsj204ng35c5k@group.calendar.google.com","client_id":"6","color":"18","name":"Test"},{"display_name":"navasardyan.edgar@outlook.com","owner_account":"navasardyan.edgar@outlook.com","client_id":"7"}]
错误消息:
json.decoder.JSONDecodeError: Invalid \escape: line 1 column 563 (char 562)