Dias
Dias

Reputation: 43

String decoding of format \x123 in Python

Do anybody know that type of string?
And how to convert it to readable format in Python?
This data from log file of the mobile app (it might be in Russian)

"title":"\x{41E}\x{442}\x{441}\x{440}\x{43E}\x{447}\x{43A}\x{430} \x{43F}\x{43E} \x{43A}\x{440}\x{435}\x{434}\x{438}\x{442}\x{443}"

Thanks ahead!

Upvotes: 0

Views: 300

Answers (2)

Błotosmętek
Błotosmętek

Reputation: 12927

import json

data = r'"\x{41E}\x{442}\x{441}\x{440}\x{43E}\x{447}\x{43A}\x{430} \x{43F}\x{43E} \x{43A}\x{440}\x{435}\x{434}\x{438}\x{442}\x{443} "'

print(json.loads(data.replace('{','').replace('}','').replace('x', 'u0')))

…and the output is Отсрочка по кредиту.

Upvotes: 1

Daweo
Daweo

Reputation: 36680

For me it does look like hex-codes of characters, I would extract codes, treat them as base-16 integers and convert to characters. That is

title = r"\x{41E}\x{442}\x{441}\x{440}\x{43E}\x{447}\x{43A}\x{430} \x{43F}\x{43E} \x{43A}\x{440}\x{435}\x{434}\x{438}\x{442}\x{443}"
codes = [code.strip('{} ') for code in title.split(r"\x") if code]
characters = [chr(int(code, 16)) for code in codes]
output = ''.join(characters)
print(output)

Output:

Отсрочкапокредиту

Upvotes: 1

Related Questions