简体   繁体   English

使用Python 3.5删除xml标签

[英]Remove xml tags using Python 3.5

So I'm new using python. 因此,我是使用python的新手。 I'm trying to remove an xml tag from an xml document. 我正在尝试从xml文档中删除xml标记。 Trying to remove ALL of <tag2> and </tag2> tags, but keep the "foo" and "bar". 尝试删除所有<tag2></tag2>标签,但保留“ foo”和“ bar”。 Suggestions? 有什么建议吗? Trying to avoid lxml 试图避免lxml

  <entry name="xml">
    <tag>
      <tag2>foo</tag2>
    </tag>
    <tag3>
      <tag2>bar</tag2>
    </tag3>
    <tag4>
      <tag2>foo</tag2>
    </tag4>
    <tag5>
      <tag2>bar</tag2>
    </tag5>
  </entry> 

EDIT: Here's what I need the output to be 编辑:这就是我需要的输出是

entry name="xml">
    <tag>
      foo
    </tag>
    <tag3>
      bar
    </tag3>
    <tag4>
      foo
    </tag4>
    <tag5>
      bar
    </tag5>
  </entry>

You could iterate over the element tree with xml. 您可以使用xml遍历元素树。 This creates a list of all the tags with text in them. 这将创建所有标签的列表,其中包含文本。

import xml.etree.ElementTree as ET

tree = ET.parse('x.xml')
root = tree.getroot()

text = []
for child in tree.iter():
    if '\n' not in child.text:
        text.append(child.text) 

Or a simpler statement from David Zemens 或是David Zemens的简单陈述

text = [child.text for child in tree.iter() if not child.text.strip() == '']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM