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

Python:XPath 在 ElementTree 中不可用

Python: xPath not available in ElementTree(Python:XPath 在 ElementTree 中不可用)
本文介紹了Python:XPath 在 ElementTree 中不可用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試使用 ElementTreeiterparse() 解析 iTunes 播放列表,但出現(xiàn)以下錯誤:

I am trying to parse iTunes Playlist by using iterparse() of ElementTree but getting following error:

AttributeError: 'Element' object has no attribute 'xpath'

代碼如下:

import xml.etree.ElementTree as ET
context = ET.iterparse(file,events=("start", "end"))
    # turn it into an iterator
    context = iter(context)
    # get the root element
    event, root = context.next()
    for event, elem in context:
        z = elem.xpath(".//key")
        elem.clear()
        root.clear()
    print z

我做錯了什么?文件太大,所以我必須使用 iterparse().

What I am doing wrong? File is too big so I have to use iterparse() anyway.

推薦答案

xml.etree.ElementTree 對 XPath 表達式提供有限的支持 為其 Element查找findallfindtext 方法(沒有 xpath 方法:這就是您收到錯誤的原因).

xml.etree.ElementTree provides limited support for XPath expressions for its Element class find, findall and findtext methods (there's no xpath method: that's why you are getting an error).

此外,如果您在元素上調(diào)用 clear() 以節(jié)省使用的內(nèi)存,則只需在完成處理該元素及其所有元素之后 執(zhí)行此操作孩子們.

Also, if you call clear() on an element to conserve used memory, you need to do it only after you've finished processing the element and all its children.

因此,您需要將代碼更改為類似于以下內(nèi)容:

Therefore, you need to to change your code to something similar to the following:

for event, elem in context:
    for child in elem.findall(".//key"):
        # process child
    elem.clear()
    root.clear()

這篇關(guān)于Python:XPath 在 ElementTree 中不可用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 亚洲精品乱码久久久久久 | 韩国三级av | 亚洲免费成人 | 四虎影院最新地址 | 超碰精品在线 | 黄色一节片 | 黄色在线观看免费 | 国产另类xxxxhd高清 | 在线播放黄色 | 日韩一级免费视频 | 97精品在线视频 | 国产精品日韩在线 | 一区二区三区成人 | 97福利视频 | 日韩欧美国产成人 | 欧美日韩中文在线 | 国产一级生活片 | 亚洲精品字幕在线观看 | 一级真人毛片 | 国产一级免费视频 | 日韩欧美在线视频 | 九九精品在线观看 | 天堂综合网 | 国模一区二区 | 亚洲视频在线看 | 中文字幕自拍偷拍 | 又色又爽又黄gif动态图 | 国产吃瓜黑料一区二区 | 亚洲一区二区精品视频 | a级片在线播放 | 久操精品视频 | 欧美激情亚洲 | 欧美成人一级 | 欧美在线网站 | 午夜爱| 九九视频在线 | 国产午夜精品久久 | www.国产精品 | 97视频在线观看免费 | www.日韩| 欧美亚洲天堂 |