Read hex characters and convert them to utf-8 with python 3

I have a data.txt file containing this line:

M\xc3\xbchle\x0astra\xc3\x9fe

      

Now the file needs to be read and the hexadecimal code is interpreted as utf-8.

So far this is my attempt:

#!/usr/bin/python3

import os
import sys

with open("data.txt") as f:
    for line in f.readlines():
        print( bytes(line, 'utf-8').decode("unicode_escape"))

      

The result converts to newline (\ x0a), but does not work with utf-8 multibyte characters:

Mรƒยผhle
straรƒe

      

+3


source to share


1 answer


Try



line = line.decode('unicode_escape').encode('latin-1').decode('utf8')

      

+2


source







All Articles