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的更多相关文章

  1. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  2. Codeforces Round #605 (Div. 3) D. Remove One Element(DP)

    链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...

  3. CF1272D. Remove One Element 题解 动态规划

    题目链接:http://codeforces.com/contest/1272/problem/D 题目大意: 给你一个长度为 \(n\) 的数组,你最多删除一个元素(也可以不删),求此条件限制下的最 ...

  4. jQuery之empty、remove、detach

    三者都有把元素移除的作用,但细微的差别,造就了它们的使命不同. 最权威的解释当然是jQuery_API咯,下面是API中关于他三儿的部分截取. 一.empty: This method removes ...

  5. 402. Remove K Digits

    (English version is after the code part) 这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进. 用这个来说: 1 2 4 3 2 2 1 ...

  6. [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  7. Jquery empty() remove() detach() 方法的区别

    方法简介: empty() This method removes not only child (and other descendant) elements, but also any text ...

  8. jQuery object and DOM Element

    They're both objects but DOMElements are special objects. jQuery just wraps DOMElements in a Javascr ...

  9. Codeforces 900C. Remove Extra One(暴力)

    You are given a permutation p of length n. Remove one element from permutation to make the number of ...

随机推荐

  1. 「日常训练」 不容易系列之(3)—— LELE的RPG难题 (HDU 2045)

    题目简述 有排成一行的n" role="presentation">nn个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何 ...

  2. App测试基本流程详解

    1 APP测试基本流程 1.1流程图 1.2测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间. 1.3测试资 ...

  3. 为Zabbix配置RabbitMQ监控模板

    RabbitMQ的配置参考 https://github.com/jasonmcintosh/rabbitmq-zabbix 简而言之,具体分为几个步骤: 1. 将脚本文件(scripts文件夹)和配 ...

  4. C指针分析详解

    局部变量和全局变量初始化区别: 局部变量,在未初始化情况下,初值为随机值.C规范对该初值并没有做规定,具体实现由编译器决定.如VC/VS等编译器,会将初始值值为0xCCCCCCCC, 而GCC等编译器 ...

  5. [Effective Python] 用Pythonic方式来思考

    Effective Python chap.1 用Pythonic方式来思考 Pythonic: 一门语言的编程习惯是由用户来确立的. 1. 确认自己所使用的Python版本 2. 遵循PEP8风格指 ...

  6. 软工实践 - 第二十一次作业 BETA 版冲刺前准备

    软工 · BETA 版冲刺前准备(团队) 过去存在的问题 组员之间缺乏沟通,前后端缺乏沟通协作 组员积极性不高 基础知识不够扎实 手动整合代码效率过低 我们已经做了哪些调整/改进 通过会议加强组员之间 ...

  7. jquery中ajax的使用(java)

    AJAX方式  js:界面 var prjContextPath='<%=request.getContextPath()%>'; $(document).ready(function() ...

  8. 每个分组函数相当于一个for循环 将集合的变量不断遍历

    每个分组函数相当于一个for循环  将集合的变量不断遍历

  9. Python数据分析(一)pandas数据切片

    1.获取行或列数据 loc——通过行标签索引行数据 iloc——通过行号索引行数据 ix——通过行标签或者行号索引行数据(基于loc和iloc 的混合) 同理,索引列数据也是如此! import pa ...

  10. 2017 Multi-University Training Contest - Team 3 Kanade's trio(字典树+组合数学)

    题解: 官方题解太简略了orz 具体实现的方式其实有很多 问题就在于确定A[j]以后,如何找符合条件的A[i] 这里其实就是要提前预处理好 我是倒序插入点的,所以要沿着A[k]爬树,找符合的A[i] ...