[Python] ElementTree and unicode

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have this char in an xml file:

    <data>
    <products>
    <color>fumè</color>
    </product>
    </data>


    I try to generate an instance of ElementTree with the following code:

    string_data = open('file.xml')
    x = ElementTree.fromstring(unicode(string_data.encode('utf-8')))


    and I get the following error:

    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 185: ordinal not in range(128)


    (NOTE: The position is not exact, I sampled the xml from a larger one).

    How to solve it? Thanks

    Continue reading...

Compartilhe esta Página