2

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?

2
  • 2
    Have you tried specifying encoding='utf-8' in your open? Commented Mar 4, 2018 at 12:58
  • Yes, it prints \xce\x97 Commented Mar 4, 2018 at 13:00

1 Answer 1

2

For decoding problems:

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

'Η'
Sign up to request clarification or add additional context in comments.

Comments

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.