[英]xml parsing and unicode (again)
xml標頭:
<?xml version="1.0" encoding="UTF-8"?><points>
xml數據段:
<point>
<id>1781</id><lon>43.245766666667</lon><lat>56.636883333333</lat>
<type>vert</type><last_update>2016-11-18 22:55:11</last_update>
<active>1</active><verified>1</verified><international>0</international><name>Vеrshilovo</name><name_ru>Вершилово</name_ru><city/><belongs>АОН</belongs><inde
碼:
tree = ET.parse(XMLFIL)
root = tree.getroot()
allpoints=root.findall('point')
for point in allpoints:
id=point.find('id').text
name=point.find('name').text.encode('utf8')
print name
這將以“ AttributeError:'NoneType'對象沒有屬性'encode'”來獎勵我序數不在范圍內(128)'
注意,錯誤與“ Vershilovo”的“ e”有關:看起來像這樣,但是xml數據的十六進制轉儲給出了
00000000 56 e5 72 73 68 69 6c 6f 76 6f 0a |V.rshilovo.|
我發現了幾個相關問題,但沒有一個給我解決方案。 根本原因很可能是我的xml數據編碼不正確,但是我無法控制它。 我可以忍受不得不將非法值重置為一些默認值,例如“ ???” 等等。
某些項目似乎沒有text
屬性。 如果text
為None
,則可以使用try-except塊,也可以使用默認值,例如:
name = (point.find('name').text or '').encode('utf8')
另一個示例,使用if語句:
name = point.find('name').text
if name:
name = name.encode('utf8')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.