用python的lxml剥离内联标签 [英] stripping inline tags with python's lxml

查看:99
本文介绍了用python的lxml剥离内联标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须处理xml文档中的两种内联标签.第一种类型的标签包含了我想保留在它们之间的文本.我可以用lxml来解决这个问题

I have to deal with two types of inline tags in xml documents. The first type of tags enclose text that I want to keep in-between. I can deal with this with lxml's

etree.tostring(element, method="text", encoding='utf-8')

第二种类型的标签包含我不想保留的文本.如何摆脱这些标签及其文字?如果可能的话,我宁愿不使用正则表达式.

The second type of tags include text that I don't want to keep. How can I get rid of these tags and their text? I would prefer not to use regular expressions, if possible.

谢谢

推荐答案

我认为 strip_elements 是在每种情况下您想要什么.例如,此脚本:

I think that strip_tags and strip_elements are what you want in each case. For example, this script:

from lxml import etree

text = "<x>hello, <z>keep me</z> and <y>ignore me</y>, and here's some <y>more</y> text</x>"

tree = etree.fromstring(text)

print etree.tostring(tree, pretty_print=True)

# Remove the <z> tags, but keep their contents:
etree.strip_tags(tree, 'z')

print '-' * 72
print etree.tostring(tree, pretty_print=True)

# Remove all the <y> tags including their contents:
etree.strip_elements(tree, 'y', with_tail=False)

print '-' * 72
print etree.tostring(tree, pretty_print=True)

...产生以下输出:

... produces the following output:

<x>hello, <z>keep me</z> and <y>ignore me</y>, and
here's some <y>more</y> text</x>

------------------------------------------------------------------------
<x>hello, keep me and <y>ignore me</y>, and
here's some <y>more</y> text</x>

------------------------------------------------------------------------
<x>hello, keep me and , and
here's some  text</x>

这篇关于用python的lxml剥离内联标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆