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

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

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

問題描述

我需要幫助來了解為什么使用 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 文件包含阿拉伯字符.

任務(wù):打開并解析 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)

結(jié)果 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)

結(jié)果 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 文件必須在第一行包含足夠的信息來處理解析器的解碼.如果缺少標(biāo)頭,解析器必須假定使用 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 標(biāo)頭,所以解析器負(fù)責(zé)進(jìn)行所有解碼.

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

您的第一次嘗試失敗了,因為 Python 試圖再次編碼 Unicode 值,以便解析器可以按預(yù)期處理字節(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.

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

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

相關(guā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 響應(yīng)并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
Large XML File Parsing in Python(Python 中的大型 XML 文件解析)
主站蜘蛛池模板: 99黄色| 国产精品日韩在线 | 永久免费看mv网站入口亚洲 | 日本不卡高字幕在线2019 | 日韩和的一区二区 | 日日av| 亚洲精品免费在线 | 国产日韩欧美视频 | 福利在线 | 久久久成人精品 | 在线成人小视频 | 欧美三级欧美一级 | 人人爽夜夜爽 | 一区二区免费在线观看 | 国产中文字幕在线播放 | 久久合 | 成人激情视频网 | 免费毛片网站 | 久久香蕉网| 日本国产在线观看 | 嫩草在线观看 | 欧美视频一区二区三区 | 亚洲精品免费观看 | 亚洲成在线 | 欧美一区在线视频 | 亚洲精品在线免费 | 黄色片观看 | 日韩精品无 | 成人免费看片39 | 免费黄色小网站 | www.av在线视频| 99国产精品99久久久久久 | 激情综合久久 | 午夜视频网站 | 欧美精品在线播放 | 国产精品手机在线观看 | 97av在线视频 | 欧美不卡一区二区三区 | 国产精品一级二级三级 | 亚洲乱码在线 | 久久久二区 |