久久久久久久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 文件解析)
主站蜘蛛池模板: 三区在线 | 天天操天天操 | 亚洲精品一区中文字幕乱码 | 日本色综合 | 欧美2区 | 亚洲九色 | 国产午夜精品一区二区三区嫩草 | 国产一区二区在线免费 | 我我色综合 | 婷婷激情在线 | 日韩精品一区二区三区在线播放 | www.婷婷| av网站免费在线观看 | a级大毛片| 精品国产一区二区在线 | 操久久 | 日韩免费av | 国产草草视频 | 免费看国产精品视频 | 亚洲高清在线观看 | 欧美在线天堂 | 日韩精品区| 国产色婷婷久久99精品91 | 亚洲成人精品在线 | 一级做a爰片性色毛片视频停止 | 2019天天操 | 国产精品免费在线 | 五月婷婷视频 | 黄色一级毛片免费看 | 欧美综合国产精品久久丁香 | 成人免费看 | 91久久精品 | 精品国产乱码久久久久久老虎 | 毛片国产 | 国产一区二区久久 | 国产精品亚洲精品日韩已方 | 欧美一级特黄aaa大片在线观看 | 国产精品国产精品国产专区不片 | 国产激情视频网站 | av电影一区二区 | 国产日韩精品一区 |