【问题标题】:Inserting wrapping child element with lxml使用 lxml 插入包装子元素
【发布时间】:2018-10-11 08:14:08
【问题描述】:

我试图在一个元素中插入一个子元素,该元素与它们的前父元素具有相同的文本,因此是一个包装子元素。基本示例,即我的输入:

<p>
    <s>text</s>
    <s>text</s>
    <s>text</s>
</p>

我想要这样的东西(anno 的内容来自 NLP 工具,不是很重要):

<p>
    <s>
        <orig>text</orig>
        <anno>token1</anno>
    </s>
    <s>
        <orig>text</orig>
        <anno>token2</anno>
    </s>
    <s>
        <orig>text</orig>
        <anno>token3</anno>
    </s>
</p>

anno 元素会更容易,我没有得到的是在里面创建一个包含曾经拥有的文本的子元素。

使用不同的元素函数(addprevious、addnext、append、insert)我可以做出如下的事情:

    <s>text<orig/></s>
    <s>text<orig>text</orig></s>
    <orig><s>text</s></orig>

但这都不是我想要的。我必须使用替换吗?在这种情况下,如何?谢谢!

【问题讨论】:

    标签: python xml lxml


    【解决方案1】:

    试试这个:

    import sys
    
    from lxml import etree
    
    tree = etree.parse(open("/tmp/so.xml"))
    
    # Make sure you have enough annos to match the existing <s> elements.
    annos = ["token1", "token2", "token3"]
    
    for i, s in enumerate(tree.xpath("//s")):
        # remember the text of the <s>
        text = s.text
    
        # remove the <s> from its parent
        parent = s.getparent()
        parent.remove(s)
    
        # create a new <s>
        new_s = etree.SubElement(parent, "s")
    
        # create the <orig>, set the remembered text on it
        orig = etree.SubElement(new_s, "orig")
        orig.text = text
    
        # create the <anon>, set the token on it
        annon = etree.SubElement(new_s, "anon")
        annon.text = annos[i]
    
    with open("/tmp/out.xml", "wb") as f:
        tree.write(f, pretty_print=True)
    

    输出:

    <p>
      <s>
        <orig>text</orig>
        <anon>token1</anon>
      </s>
      <s>
        <orig>text</orig>
        <anon>token2</anon>
      </s>
      <s>
        <orig>text</orig>
        <anon>token3</anon>
      </s>
    </p>
    

    【讨论】:

    • 好的,保存文本,获取父元素,删除元素,然后创建新的子元素。完美,非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多