Search code examples
python-3.4python-unicode

python3 bytes string encoding


i have this code:

res = conn.getresponse()
data = res.read()

doc = xmltodict.parse(data)

risultati = doc['result']['data']

mieiris = json.loads(risultati)

for k in mieiris['Headword']['Component']:
    try:
        print(k['Text'])
    except KeyError:
        pass
    except UnicodeEncodeError:
        uhm = k['Text'].encode("utf-8")
        print(uhm.decode("unicode_escape"))

that returns me this result:

b'ci\xc3\xa0-o'
inter.
si usa come saluto amichevole e confidenziale quando ci si incontra o ci si lascia
b'Dal ven. {\\i s{#c-v-r#}iao}, propr. \xe2\x80\x98(sono vostro) schiavo\xe2\x80\x99'

now: I can't figure out how to properly display the last string with utf-8 encoding. Do you have any hints for me?


Solution

  • I've resolved with unidecode module.

    So my last for is now:

    for k in mieiris['Headword']['Component']:
        try:
            print(k['Text'])
        except KeyError:
            pass
        except UnicodeEncodeError:
            print(unidecode(k['Text']))
    

    Hope that this will help someone else :)