也就是说,所有文本和子标签,没有元素本身的标签?
Having
<p>blah <b>bleh</b> blih</p>
I want
blah <b>bleh</b> blih
element.text 返回“blah”,etree.tostring(element) 返回:
<p>blah <b>bleh</b> blih</p>
ElementTree 工作得很好,你必须自己组装答案。像这样的东西...
"".join( [ "" if t.text is None else t.text ] + [ xml.tostring(e) for e in t.getchildren() ] )
感谢 JV amd PEZ 指出错误。
Edit.
>>> import xml.etree.ElementTree as xml
>>> s= '<p>blah <b>bleh</b> blih</p>\n'
>>> t=xml.fromstring(s)
>>> "".join( [ t.text ] + [ xml.tostring(e) for e in t.getchildren() ] )
'blah <b>bleh</b> blih'
>>>
不需要尾巴。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)