A. python xml.etree 中findall('./item')問題

./item 與 item 區別
對於find操作來說,這二者沒區別
如果是其他方法,類似C裡面的system,那麼就有區別了.
後者會查找env path,不一定能找到`pwd`/item
望點贊

B. 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)

C. python解析xml:xml.etree.ElementTree似乎存在bug

<?xml version = "1.0" encoding = "utf-8"?>
<root>
<body name="lyc">
<age>110</age>
</body>
<body name = "l" age = "10">
</body>
</root>

D. python2.7中lxml安裝後無法導入etree求教

pythonlxml


  • 在lxml文件夾下etree是pyd後綴,是libxml2,libxslt這兩個沒安裝好么

用正則吧。用過xpath 感覺還是正則靠譜

先看安裝lxml的時候有錯誤信息

E. lxml4.3怎麼調用etree 怎麼用

解決辦法:
import lxml.htmletree = lxml.html.etree12

這樣就可以使用etree了

F. 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')]

G. 在python中用ElementTree提取XML中的內容

fromxml.etreeimportElementTree
str_=''#文件中的xml字元串
xml_obj=ElementTree.fromstring(str_)

然後通過對xml_obj進行操作,xml_obj本身也是一個xml節點回。

xml_obj.getchildren() 獲取根節點的子節點列表答

xml_obj.findall(node_name) 搜索xml_obj節點下名為node_name的所有節點

xml_obj.tag 節點的標簽

xml_obj.text 節點的文本信息 ,本例中可以獲得K這個文本。

xml_obj.tail 節點尾部的文本信息,本例中獲取Channel Regulator KCR1 Suppresses Heart Rhythm by Molating the Pacemaker Current I
就需要搜索到標簽為sup的節點,然後取節點的tail文本獲得。

H. 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

I. 如何使用Python和xml.etree.ElementTree解析xml文件獲取其節點

基於文本文檔(Markdown) 設想好需要的基本需要的表、欄位、類型;內
使用 Rails Migration 隨著功能容的開發逐步創建表;
隨著細節功能的開發、需求,逐步增加欄位,刪除欄位,或者調整欄位類型;
第一個 Release 的時候清理 Migrations 合並成一個;
隨著後期的改動,逐步增加、修改、刪除欄位或表。
基本上我的所有項目都是這么搞的,這和項目是否復雜無關。
所以我前面為什麼說思路需要轉變。

J. python怎麼向已經存在的xml文件中追加填入數據

直接操作文件肯定不現實,只能先使用python自帶模塊xml.etree 解析xml,然後找到對應的節點把數據添加進去,重新寫入文件。