久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 ElementTree 正確解析 utf-8 xml?

How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
本文介紹了如何使用 ElementTree 正確解析 utf-8 xml?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要幫助來了解為什么使用 xml.etree.ElementTree 解析我的 xml 文件* 會產(chǎn)生以下錯誤.

I need help to understand why parsing my xml file* with xml.etree.ElementTree produces the following errors.

*我的測試 xml 文件包含阿拉伯字符.

任務:打開并解析 utf8_file.xml 文件.

我的第一次嘗試:

import xml.etree.ElementTree as etree
with codecs.open('utf8_file.xml', 'r', encoding='utf-8') as utf8_file:
    xml_tree = etree.parse(utf8_file)

結果 1:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 236-238: ordinal not in range(128)

我的第二次嘗試:

import xml.etree.ElementTree as etree
with codecs.open('utf8_file.xml', 'r', encoding='utf-8') as utf8_file:
    xml_string = etree.tostring(utf8_file, encoding='utf-8', method='xml')
    xml_tree  = etree.fromstring(xml_string)

結果 2:

AttributeError: 'file' object has no attribute 'getiterator'

請解釋上述錯誤并評論可能的解決方案.

Please explain the errors above and comment on the possible solution.

推薦答案

將字節(jié)解碼留給解析器;先解碼:

Leave decoding the bytes to the parser; do not decode first:

import xml.etree.ElementTree as etree
with open('utf8_file.xml', 'r') as xml_file:
    xml_tree = etree.parse(xml_file)

一個 XML 文件必須在第一行包含足夠的信息來處理解析器的解碼.如果缺少標頭,解析器必須假定使用 UTF-8.

An XML file must contain enough information in the first line to handle decoding by the parser. If the header is missing, the parser must assume UTF-8 is used.

因為保存這些信息的是 XML 標頭,所以解析器負責進行所有解碼.

Because it is the XML header that holds this information, it is the responsibility of the parser to do all decoding.

您的第一次嘗試失敗了,因為 Python 試圖再次編碼 Unicode 值,以便解析器可以按預期處理字節(jié)字符串.第二次嘗試失敗,因為 etree.tostring() 期望解析樹作為第一個參數(shù),而不是 unicode 字符串.

Your first attempt failed because Python was trying to encode the Unicode values again so that the parser could handle byte strings as it expected. The second attempt failed because etree.tostring() expects a parsed tree as first argument, not a unicode string.

這篇關于如何使用 ElementTree 正確解析 utf-8 xml?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
Large XML File Parsing in Python(Python 中的大型 XML 文件解析)
主站蜘蛛池模板: 午夜一级大片 | 亚洲欧美成人影院 | 久久综合伊人 | 一区二区播放 | 欧美日韩一区二区三区视频 | 成人亚洲视频 | 日韩福利片 | 国产一区| 黄视频免费在线 | 97精品一区二区 | 亚洲在线一区二区 | 日韩在线一区二区三区 | 一区中文字幕 | 欧美日韩成人在线 | 久久色视频 | 欧美视频一区二区三区 | 精品日韩一区二区三区av动图 | 日韩在线视频网址 | 综合国产 | 99精品久久久久久 | 中文字幕第一页在线 | 能看的av网站 | 午夜免费在线电影 | 人人看人人草 | 91在线观看视频 | 欧美伦理一区 | 久久国产成人午夜av影院武则天 | 亚州精品天堂中文字幕 | 97色在线视频 | 男女污污网站 | 鲁一鲁资源影视 | 三级成人在线 | 免费观看羞羞视频网站 | 日本在线你懂的 | 99久久久国产精品 | 91黄色片免费看 | 天堂一区二区三区 | 成人国产精品久久 | 日韩精品在线播放 | 精品欧美一区二区精品久久 | 亚洲成人精品久久久 |