0

I am trying to parse unicode string in python and run into Invalid \escape error. I have seen this post, but it didn't help either.

Here's my code:

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
    # ...

And here's the value of params:

[{"display_name":"My calendar","owner_account":"My calendar","client_id":"1","name":"My calendar"},{"display_name":"[email protected]","owner_account":"[email protected]","client_id":"2","color":"15","name":"[email protected]"},{"display_name":"Contacts","owner_account":"#[email protected]","client_id":"3","color":"9","name":"Contacts"},{"display_name":"Holidays in Armenia","owner_account":"en.am#[email protected]","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#[email protected]","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":"[email protected]","client_id":"6","color":"18","name":"Test"},{"display_name":"[email protected]","owner_account":"[email protected]","client_id":"7"}]

The error message as is:

json.decoder.JSONDecodeError: Invalid \escape: line 1 column 563 (char 562)

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.