5

Consider I have a Unicode string (Not the real unicode but the string that looks like unicode). and I want to get it's utf-8 variant. How can I do it in Python? For example If I have String like:

title = "\\u10d8\\u10e1\\u10e0\\u10d0\\u10d4\\u10da\\u10d8 == \\u10d8\\u10d4\\u10e0\\u10e3\\u10e1\\u10d0\\u10da\\u10d8\\u10db\\u10d8"

How Can I do it so that I get its utf-8 variant (Georgian symbols):

ისრაელი == იერუსალიმი

To say it simply I want to Have code like:

title = "\\u10d8\\u10e1\\u10e0\\u10d0\\u10d4\\u10da\\u10d8 == \\u10d8\\u10d4\\u10e0\\u10e3\\u10e1\\u10d0\\u10da\\u10d8\\u10db\\u10d8"
utfTitle = title.TurnToUTF()
print(utfTitle)

And I want this code to have output:

ისრაელი == იერუსალიმი

4
  • See stackoverflow.com/questions/1473577/… Commented Dec 28, 2017 at 11:04
  • I think this link can help you... Commented Dec 28, 2017 at 11:04
  • @MosheSlavin It doesn't help Commented Dec 28, 2017 at 11:07
  • @ozking It doesn't help Commented Dec 28, 2017 at 11:07

3 Answers 3

7

You can use the unicode-escape codec to get rid of the doubled-backslashes and use the string effectively.

Assuming that title is a str, you will need to encode the string first before decoding back to unicode(str).

>>> t = title.encode('utf-8').decode('unicode-escape')
>>> t
'ისრაელი == იერუსალიმი'

If title is a bytes instance you can decode directly:

>>> t = title.decode('unicode-escape')
>>> t
'ისრაელი == იერუსალიმი'
Sign up to request clarification or add additional context in comments.

Comments

6

Here, you go. Just use decode method and apply unicode_escape

For Python 2.x

title = "\\u10d8\\u10e1\\u10e0\\u10d0\\u10d4\\u10da\\u10d8 == \\u10d8\\u10d4\\u10e0\\u10e3\\u10e1\\u10d0\\u10da\\u10d8\\u10db\\u10d8"
utfTitle = title.decode('unicode_escape')
print(utfTitle)

#output :ისრაელი == იერუსალიმი

For python 3.x

title = "\\u10d8\\u10e1\\u10e0\\u10d0\\u10d4\\u10da\\u10d8 == \\u10d8\\u10d4\\u10e0\\u10e3\\u10e1\\u10d0\\u10da\\u10d8\\u10db\\u10d8"
print(title.encode('ascii').decode('unicode-escape'))

Comments

0

let assume the unicode be str type and convert using decode and unicode-escape method

title="\\u10d8\\u10e1\\u10e0\\u10d0\\u10d4\\u10da\\u10d8 == \\u10d8\\u10d4\\u10e0\\u10e3\\u10e1\\u10d0\\u10da\\u10d8\\u10db\\u10d8"

res1 = title.encode('utf-8')

res2 = res1.decode('unicode-escape')

print(res2)

1 Comment

UnicodeEncodeError: 'cp932' codec can't encode character '\u10d8' in position 0: illegal multibyte sequence

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.