Question

I have a sequence of UTF-8 characters, for example something like this:

\xd8\xa2\xd8\xb4\xd9\x86\xd8\xa7\xdb\x8c\xdb\x8c \xd8\xa8\xd8\xa7 \xd8\xa2\xd8\xb1\xd9\x85\xd8\xa7\xd9\x86 \xd9\xbe\xd8\xb1\xd9\x88\xda\x98\xd9\x87 \xd9\x84\xd8\xba\xd8\xaa \xd9\x86\xd8\xa7\xd9\x85\xd9\x87 \xd8\xa2\xd8\xb2\xd8\xa7\xd8\xaf

which I know is from a Farsi website and should represent some Farsi words. How can I represent this sequence with Farsi characters?

I am using Python for my work.

Was it helpful?

Solution

You just need to treat the sequence of bytes as a sequence of bytes by preceding the literal with the letter b, and then decoding as UTF-8. Like so:

$ python3
>>> text = b'\xd8\xa2\xd8\xb4\xd9\x86\xd8\xa7\xdb\x8c\xdb\x8c \xd8\xa8\xd8\xa7 \xd8\xa2\xd8\xb1\xd9\x85\xd8\xa7\xd9\x86 \xd9\xbe\xd8\xb1\xd9\x88\xda\x98\xd9\x87 \xd9\x84\xd8\xba\xd8\xaa \xd9\x86\xd8\xa7\xd9\x85\xd9\x87 \xd8\xa2\xd8\xb2\xd8\xa7\xd8\xaf'
>>> text.decode('utf-8')
'آشنایی با آرمان پروژه لغت نامه آزاد'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top