how to remove an element in lxml
2015-07-29 13:32
218 查看
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)
相关文章推荐
- Java的四种基本排序
- 杨辉三角一维数组实现
- sqlite中文乱码问题原因分析及解决
- UISegmentControl 和UIslider
- Arcgis Server发布GP服务
- iOS开发之如何获取各种控件的输入值
- 为什么软件工程师应该养成写作的习惯?
- windows线程不安全系统函数列表
- 浅谈用力不用脑的程序员:你为什么蹉跎这么多年
- 《大圣归来》观后感
- STLINK找不到debug session的解决办法
- SVN服务器搭建和使用(三)
- TortoiseSVN客户端重新设置用户名和密码
- awk——getline
- git 修改上次提交信息 与 撤销此操作.
- Android强制切换横屏竖屏不起作用的解决办法
- ORACLE統計信息收集
- linux下解压缩*.tar.xz方法
- SVN服务器搭建和使用(二)
- 煎饼侠大鹏如何屌丝逆袭:用互联网卖笑