我正在使用ElementTree
修改以下XML:
<li>
<p>Some stuff goes in <b>bold</b> here </p>
</li>
我想从<li>
元素中删除所有<p>
,但保留内容。
这样:
<li>Some stuff goes in <b>bold</b> here</li>
我目前使用的是以下代码,它在简单的情况下(没有文本/尾巴等)有效:
# strip <p> from <li> elements
liElements = rootNode.findall('.//li')
for elem in liElements:
para = elem.find(".//p")
for child in para:
elem.append(child)
elem.text = para.text
elem.remove(para)
必须有一种更简单的方法来剥离格式化标签...我希望?
可能最简单的方法是不使用来处理推荐答案,而是使用BeautifulSoup;该库通过.unwrap()
method:
for elem in soup.find_all('li'):
for para in elem.find_all('p'):
para.unwrap()
上一篇:对于有效的XML树,Python getChild()不起作用不起作用、有效、Python、XML
下一篇:如何SqlConnection的管理的IsolationLevel?SqlConnection、IsolationLevel