Skip to content Skip to sidebar Skip to footer

How To Convert String Containing Unicode Escape \u#### To Utf-8 String

I am trying this since morning. My sample.txt choice = \u9078\u629e Code: with open('sample.txt', encoding='utf-8') as f: for line in f: print(line) print('選

Solution 1:

If you read it from a file, just give the encoding when opening:

withopen('test.txt', encoding='unicode-escape') as f:    
    a = f.read()
print(a)

# choice = 選択

with test.txt containing:

choice = \u9078\u629e

If you already had your text in a string, you could have converted it like this:

a = "choice = \\u9078\\u629e"
a.encode().decode('unicode-escape')
# 'choice = 選択'

Post a Comment for "How To Convert String Containing Unicode Escape \u#### To Utf-8 String"