【发布时间】:2017-01-16 04:16:03
【问题描述】:
我有这个 utf-8 编码的 XML 文件。
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<Cikkszam>00008</Cikkszam>
<EAN/>
<Megjegyzes>BISK</Megjegyzes>
<Leiras1>Bisk Ontario, Dakota szappantartóhoz</Leiras1>
<Leiras2>műanyag betét</Leiras2>
<CikkTipus>07 </CikkTipus>
<ME_>db </ME_>
<Tipus>Választható</Tipus>
<A_ar>338</A_ar>
<B_ar>0</B_ar>
<C_ar>0</C_ar>
<D_ar>0</D_ar>
<E_ar>0</E_ar>
<Tenyl_keszl_>0</Tenyl_keszl_>
<Visszaig_>0</Visszaig_>
<Diszponalt>0</Diszponalt>
<Szabad_keszlet>0</Szabad_keszlet>
</Item>
</Items>
我有这个 Python 代码:
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
当我运行我的脚本文件时,我收到了下一条错误消息:
C:\Python27>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 <module>
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 <undefined>
【问题讨论】: