码迷,mamicode.com
首页 > 其他好文 > 详细

how to remove an element in lxml

时间:2015-07-29 15:27:44      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

import lxml.etree as et

xml="""
<groceries>
  <fruit state="rotten">apple</fruit>
  <fruit state="fresh">pear</fruit>
  <punnet>
    <fruit state="rotten">strawberry</fruit>
    <fruit state="fresh">blueberry</fruit>
  </punnet>
  <fruit state="fresh">starfruit</fruit>
  <fruit state="rotten">mango</fruit>
  <fruit state="fresh">peach</fruit>
</groceries>
"""

tree=et.fromstring(xml)

for bad in tree.xpath("//fruit[@state=‘rotten‘]"):
    bad.getparent().remove(bad)

print et.tostring(tree, pretty_print=True)

how to remove an element in lxml

标签:

原文地址:http://www.cnblogs.com/zhang-pengcheng/p/4685700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!