简体   繁体   中英

Python Hex values in ascii encoded string

I have a problem in python reading a string from a .txt file
File contains these data : \\xce\\x97
Encoded in ascii (Similar to "\\xce\\x97" using a python string) I want to convert it to UTF-8 encoding

file.open("file.txt", "r")
a = file.read() #a = "\\xce\\x97"
file.close()


The correct value of this string is : "Η" (Its a greek letter, capital "η") Ι can use

>>>a = b'\xce\x97'
>>>print(a.decode("utf-8"))
>>>Η

How can I do it using the varriable a?

For decoding problems:

a = "\\xce\\x97"
print(a.encode().decode('unicode-escape').encode("latin-1").decode('utf-8'))

'Η'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM