[英]How to get the text between tags in a xml, using preferably lxml
這是標簽的示例,但是我無法獲取標簽之間的文本,無法遍歷標簽,也無法獲取節點<seg>
中的node.text。 這就是為什么我要問的,所有幫助都將受到歡迎(對不起我的英語)。
<tuv>
<seg>If you want to save items in a
<bpt i="1"><Message id="Message:1T0000772343:f000012900ce8eb3:MPhS"></bpt>
<ept i="1"></Message></ept>
for which no connection has been established or in a
<bpt i="2"><Message id="Message:1T0000772343:f000012900ceac3d:pvy4"></bpt>
<ept i="2"></Message></ept>
that requires authentication, you need to connect to the library.
</seg>
</tuv>
想要的輸出:
如果要將項目保存在尚未建立連接的項目中或需要身份驗證的項目中,則需要連接到庫。
在<seg>
元素上使用.xpath("text()")
獲取所有文本節點。
此代碼顯示所需的輸出:
from lxml import etree
root = etree.parse("tuv.xml")
seg = root.find("seg")
# Get the text nodes of 'seg' as one string
text = " ".join(t for t in seg.xpath("text()"))
# Print result with unwanted whitespace removed
print " ".join(text.split())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.