简体   繁体   English

在lxml中重写ElementTree代码

[英]rewrite ElementTree code in lxml

I am writing a code to extract text from a xml file using ElementTree but I found out that lxml is giving xpath features which is more convenient. 我正在编写代码以使用ElementTree从xml文件提取文本,但是我发现lxml具有xpath功能,这更加方便。 So i want to know how this line could be rewritten in lxml 所以我想知道如何在lxml中重写这一行

if x.nodeName == 'a:pPr' and x.getAttribute('lvl') == '2' and x.hasAttribute('marL') == False:

currently I am suggested to use this.. 目前建议我使用此。

'/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/x[@type="body" and  @sz="quarter"  and  @marL]'

Hope my question is clear! 希望我的问题清楚!

I'm assuming you are already at a stage where you're ready to use an xpath. 我假设您已经处在准备使用xpath的阶段。 If not http://lxml.de/parsing.html should help. 如果没有, http://lxml.de/parsing.html应该会有所帮助。 The xpath for finding the element, you are searching for in your first line should look like this: 在第一行中用于查找元素的xpath应该看起来像这样:

"//a:pPr[@lvl=2 and not(@marL)]"

I however have to admit that I have no idea how your second snippet should find that element O_o 但是,我不得不承认,我不知道您的第二个片段应如何找到该元素O_o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM