Convert “\x” escaped string into readable string in python

Decode it first using ‘unicode-escape’, then as ‘utf8’:

a = "\\xe8\\xaa\\x9e\\xe8\\xa8\\x80"
    
decoded = a.encode('latin1').decode('unicode_escape').encode('latin1').decode('utf8')
print(decoded)

# 語言

Note that since we can only decode bytes objects, we need to transparently encode it in between, using ‘latin1’.

Leave a Comment