San*_* Hs 3 python lxml nodes removechild python-2.7
我remove element node在XML文件中找到了很多例子.这里的BUt是一个例子,我没有在stackoverflow或google上找到任何解决方案.例如:
<slide>
America
<a> 2 </a>
<b> 3 </b>
<c> 4 </c>
</slide>
<slide>
Germany
<a> 5 </a>
<b> 6 </b>
<c> 7 </c>
</slide>
Run Code Online (Sandbox Code Playgroud)
我会使用remove函数来删除元素节点,因为我使用的是lxml.但现在我必须删除"美国"和"德国",它们实际上不是元素节点而是文本.
有没有办法像任何函数一样删除这个?
我目前正在使用python lxml库.
输出应如下所示:
<slide>
<a> 2 </a>
<b> 3 </b>
<c> 4 </c>
</slide>
<slide>
<a> 5 </a>
<b> 6 </b>
<c> 7 </c>
</slide>
Run Code Online (Sandbox Code Playgroud)
使用text财产.例如:
html = '''...
<slide>
America
<a> 2 </a>
<b> 3 </b>
<c> 4 </c>
</slide>
<slide>
Germany
<a> 5 </a>
<b> 6 </b>
<c> 7 </c>
</slide>
....'''
import lxml.html
root = lxml.html.fromstring(html)
for slide in root.xpath('.//slide'):
slide.text = ''
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
495 次 |
| 最近记录: |