-1

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?

Nik
  • 107
  • 1
  • 2
  • 10
  • This all seems very unnecessary - you should only be getting `UnicodeEncodeError` if your console's locale can't (or thinks it can't) support the character you're printing. You should either fix your console or write to a file instead. Your final `encode()` / `decode()` is totally crazy. This sounds like a typical X-Y problem – Alastair McCormack Jun 10 '16 at 21:51
  • Hi @AlastairMcCormack, yes, it's totally crazy ;) I've done a lot of trials in order to obtain the string formatted in a proper way and that was my last one ;) I can't write to a file and this will be a telegram bot so the console won't be my final output. – Nik Jun 11 '16 at 10:04
  • :) ok, so you're working around a problem for a limitation in your specific console that won't exist in your final app - that's a bad idea :) Are you writing your own telegram bot (not sure how UTF-8 can be expressed in morse) or someone else's? As it stands, it's unclear what you're asking – Alastair McCormack Jun 11 '16 at 10:31
  • I've found that module 'uniencode' solves my problem :) Anyway I am writing a [Telegram](https://telegram.org/) bot. Thank you for your time! – Nik Jun 13 '16 at 06:50

1 Answers1

0

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 :)

Nik
  • 107
  • 1
  • 2
  • 10