在lxml中重写ElementTree代码 [英] rewrite ElementTree code in lxml

查看:85
本文介绍了在lxml中重写ElementTree代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码,使用ElementTree从xml文件中提取文本,但是我发现lxml具有xpath功能,这更加方便.所以我想知道如何在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. So i want to know how this line could be rewritten in lxml

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

目前,我建议使用它.

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

希望我的问题很清楚!

推荐答案

我假设您已经处在准备使用xpath的阶段.如果没有, http://lxml.de/parsing.html 应该会有所帮助. 在第一行中用于查找元素的xpath看起来应该像这样:

I'm assuming you are already at a stage where you're ready to use an xpath. If not http://lxml.de/parsing.html should help. The xpath for finding the element, you are searching for in your first line should look like this:

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

但是我不得不承认我不知道你的第二个片段应该如何找到该元素O_o

I however have to admit that I have no idea how your second snippet should find that element O_o

这篇关于在lxml中重写ElementTree代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆