pythonetree
Ⅰ python xml.etree.element從xml文件獲取文檔標題的格式,比如幾級標題
import xml.etree.ElementTree as ET
tree = ET.ElementTree('doc1.xml') ;讀取doc1.xml
root = tree.getroot()
for child in root: ;遍歷所有子結束,輸出結點的標簽,屬性
print(child.tag, child.attrib)
Ⅱ python lxml etree怎麼甩
lxml是Python語言中處理XML和html功能最豐富,最易於使用的庫。
lxml是libxml2和libxslt兩個C庫的Python化綁定,它的獨特之處在於兼顧了這些庫的速度和功能完整性,同時還具有Python API的簡介。兼容ElementTree API,但是比它更優越。
用libxml2編程就像是一個異於常人的陌生人的令人驚恐的擁抱,它看上去可以滿足你一切瘋狂的夢想,但是你的內心深處一直在警告你,你有可能會以最糟糕的方式遭殃,所以就有了lxml。
這是一個用lxml.etree來處理XML的教程,它簡單的概述了ElementTree API的主要概念,同時有一些能讓你的程序生涯更輕松的簡單的提高。
首先是導入lxml.etree的方式:
fromlxmlimportetree
為了協助代碼的可移植性,本教程中的例子很明顯可以看出,一部分API是lxml.etree在ElementTree API(由Fredrik Lundh 的ElementTree庫定義)的基礎上的擴展。
Element是ElementTree API的主要容器類,大部分XML tree的功能都是通過這個類來實現的,Element的創建很容易:
root=etree.Element("root")
element的XML tag名通過tag屬性來訪問
>>>printroot.tag
root
許多Element被組織成一個XML樹狀結構,創建一個子element並添加進父element使用append方法:
>>>root.append(etree.Element("child1"))
還有一個更簡短更有效的方法:the SubElement,它的參數和element一樣,但是需要父element作為第一個參數:
>>>child2=etree.SubElement(root,"child2")
>>>child3=etree.SubElement(root,"child3")
可以序列化你創建的樹:
>>>print(etree.tostring(root,pretty_print=True))
<root>
<child1/>
<child2/>
<child3/>
</root>
為了更方便直觀的訪問這些子節點,element模仿了正常的Python鏈:
>>>child=root[0]>>>print(child.tag)
child1
>>>print(len(root))
>>>root.index(root[1])#lxml.etreeonly!
>>>children=list(root)>>>forchildinroot:...print(child.tag)child1child2
child3
>>>root.insert(0,etree.Element("child0"))>>>start=root[:1]>>>end=root[-1:]>>>print(start[0].tag)child0>>>print(end[0].tag)child3
還可以根據element的真值看其是否有孩子節點:
ifroot:#thisnolongerworks!
print("Therootelementhaschildren")
用len(element)更直觀,且不容易出錯:
>>>print(etree.iselement(root))#testifit'ssomekindofElement
True
>>>iflen(root):#testifithaschildren
...print("Therootelementhaschildren")
Therootelementhaschildren
還有一個重要的特性,原文的句子只可意會,看例子應該是能看懂什麼意思吧。
>>>forchildinroot:...print(child.tag)child0child1child2child3>>>root[0]=root[-1]#移動了element>>>forchildinroot:...print(child.tag)child3child1child2>>>l=[0,1,2,3]>>>l[0]=l[-1]>>>l[3,1,2,3]
>>>rootisroot[0].getparent()#lxml.etreeonly!.etree,'sstandardlibrary:>>>fromimportdeep>>>element=etree.Element("neu")>>>element.append(deep(root[1]))>>>print(element[0].tag)child1>>>print([c.tagforcinroot])['child3','child1','child2']
XML支持屬性,創建方式如下:
>>>root=etree.Element("root",interesting="totally")
>>>etree.tostring(root)
b'<rootinteresting="totally"/>'
屬性是無序的鍵值對,所以可以用element類似於字典介面的方式處理:
>>>print(root.get("interesting"))
totally
>>>print(root.get("hello"))
None
>>>root.set("hello","Huhu")
>>>print(root.get("hello"))
Huhu
>>>etree.tostring(root)
b'<rootinteresting="totally"hello="Huhu"/>'
>>>sorted(root.keys())
['hello','interesting']
>>>forname,valueinsorted(root.items()):
...print('%s=%r'%(name,value))
hello='Huhu'
interesting='totally'
如果需要獲得一個類似dict的對象,可以使用attrib屬性:
>>>attributes=root.attrib
>>>print(attributes["interesting"])
totally
>>>print(attributes.get("no-such-attribute"))
None
>>>attributes["hello"]="GutenTag"
>>>print(attributes["hello"])
GutenTag
>>>print(root.get("hello"))
GutenTag
既然attrib是element本身支持的類似dict的對象,這就意味著任何對element的改變都會影響attrib,反之亦然。這還意味著只要element的任何一個attrib還在使用,XML樹就一直在內存中。通過如下方法,可以獲得一個獨立於XML樹的attrib的快照:
>>>d=dict(root.attrib)
>>>sorted(d.items())
[('hello','GutenTag'),('interesting','totally')]
Ⅲ 如何使用Python和xml.etree.ElementTree解析xml文件獲取其節點
基於文本文檔(Markdown) 設想好需要的基本需要的表、欄位、類型;內
使用 Rails Migration 隨著功能容的開發逐步創建表;
隨著細節功能的開發、需求,逐步增加欄位,刪除欄位,或者調整欄位類型;
第一個 Release 的時候清理 Migrations 合並成一個;
隨著後期的改動,逐步增加、修改、刪除欄位或表。
基本上我的所有項目都是這么搞的,這和項目是否復雜無關。
所以我前面為什麼說思路需要轉變。
Ⅳ python2.7中lxml安裝後無法導入etree求教
pythonlxml
在lxml文件夾下etree是pyd後綴,是libxml2,libxslt這兩個沒安裝好么
用正則吧。用過xpath 感覺還是正則靠譜
先看安裝lxml的時候有錯誤信息
Ⅳ python中tkinter treeview如何獲取選中的條目
定義這樣的函數去專門取tree上的值
def selectItem(a):
curItem = tree.focus()
print tree.item(curItem)
它返回一個字典類型的集合,你只要遍歷字典就可以把你需要的值取出來。。。
Ⅵ python lxml中etree.html和etree.parse有什麼區別
etree.parse直接接受來一個文檔,源按照文檔結構解析
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
etree.html可以解析html文件:
page = etree.HTML(html.lower().decode('utf-8'))
hrefs = page.xpath(u"//a")
for href in hrefs:
print href.attrib
Ⅶ python中tree=etree.html什麼意思
etree是一個對象,其有一個屬性叫html,把這個屬性的引用賦給變數tree。
Ⅷ python etree element類 實例不能動態增加屬性
tree=etree.parse("xxx.xml")
root=tree.getroot()
root.set('myattr',"123")
print(root.attrib)