ElementTree XML parsing from UTF-8 source file
<p>I have this XML file with utf-8 encoding.</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
 &lt;Items&gt;
 &lt;Item&gt;
 &lt;Cikkszam&gt;00008&lt;/Cikkszam&gt;
 &lt;EAN/&gt;
 &lt;Megjegyzes&gt;BISK&lt;/Megjegyzes&gt;
 &lt;Leiras1&gt;Bisk Ontario, Dakota szappantartÃ³hoz&lt;/Leiras1&gt;
 &lt;Leiras2&gt;mÅ±anyag betÃ©t&lt;/Leiras2&gt;
 &lt;CikkTipus&gt;07  &lt;/CikkTipus&gt;
 &lt;ME_&gt;db   &lt;/ME_&gt;
 &lt;Tipus&gt;VÃ¡laszthatÃ³&lt;/Tipus&gt;
 &lt;A_ar&gt;338&lt;/A_ar&gt;
 &lt;B_ar&gt;0&lt;/B_ar&gt;
 &lt;C_ar&gt;0&lt;/C_ar&gt;
 &lt;D_ar&gt;0&lt;/D_ar&gt;
 &lt;E_ar&gt;0&lt;/E_ar&gt;
 &lt;Tenyl_keszl_&gt;0&lt;/Tenyl_keszl_&gt;
 &lt;Visszaig_&gt;0&lt;/Visszaig_&gt;
 &lt;Diszponalt&gt;0&lt;/Diszponalt&gt;
 &lt;Szabad_keszlet&gt;0&lt;/Szabad_keszlet&gt;
 &lt;/Item&gt;
&lt;/Items&gt;
</code></pre>

<p>I have this Python code:</p>

<pre><code>from xml.etree.ElementTree import ElementTree
#import xml.etree.ElementTree

items = ElementTree().parse('proba.xml'.encode('utf8'))
#items = xml.etree.ElementTree.parse('proba.xml')

products = items.findall("Item")
for product in products:
    print product.find("Leiras1").text
</code></pre>

<p>When I run my script file I got the next error message:</p>

<pre class="lang-none prettyprint-override"><code>C:\Python27&gt;python read_xml_orig.py
Bisk Ontario, Dakota szappantartÃ³hoz
Bisk ÃllÃ³ Wc kefe Ã©s papÃ­r tartÃ³ talpas
Bisk sarok szappantartÃ³ krÃ³m
Bisk sarok szappantartÃ³ krÃ³m
Bisk Ã¼veg pohÃ¡r pÃ³t
Bisk ONTARIO Ã¼veg folyÃ©kony
Bisk ONTARIO Ã¼veg polc
Bisk ONTARIO dupla
Bisk ONTARIO  rud
Traceback (most recent call last):
  File "read_xml_orig.py", line 7, in &lt;module&gt;
    print product.find("Leiras1").text
  File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u0151' in position
 21: character maps to &lt;undefined&gt;
</code></pre>

<p><code>'proba.xml'.encode('utf8')</code> encodes the string <code>'proba.xml'</code> as UTF-8. It does nothing to the file of the same name.</p>

<p>When the file really is encoded as UTF-8 then your code will work:</p>

<pre><code>from xml.etree.ElementTree import ElementTree

doc = ElementTree().parse('proba.xml')

products = doc.findall("Item")
for product in products:
    print product.find("Leiras1").text
</code></pre>

<p>which prints this for me</p>

<pre><code>Bisk Ontario, Dakota szappantartÃ³hoz
</code></pre>

<p><strong>However</strong>, if your file is not really UTF-8 then you might get encoding errors during <code>.parse()</code>.</p>

<p>In this case you must figure out what encoding the file is in and correct the XML declaration accordingly. Hint: A likely encoding candidate for Hungarian is <code>Windows-1250</code>.</p>

python xml parsing utf-8