创建与子节点同名的多个节点

时间:2019-03-11 23:31:49

标签: python xml celementtree

我有一个文本文件,我使用bool Queue::enqueue(Node *& pTail, Data newData) { if (pTail == nullptr) { pTail = new Node(pData); this->pHead = pTail; } else { pTail->setPNext(new Node(pData)); pTail = pTail->getPNext(); } return true; } 库用python对其进行了解析。 在输入中,我有一个包含句子xml.etree.cElementTree的段落<p>,每个句子都有单词<s>,这是文本文件的样子:

<w>

在输出中,我想要以下xml文件:

This
is
my
first
sentence.
This
is
my
second
sentence.

我编写了以下python代码,这些代码为我提供了段落标签和word标签,但我不知道如何实现具有多个<p> <s> <w>this</w> <w>is</w> <w>my</w> <w>first</w> <w>sentence</w> <pc>.</pc> </s> <s> <w>this</w> <w>is</w> <w>my</w> <w>second</w> <w>sentence</w> <pc>.</pc> </s> </p> 标签的案例。句子以大写字母开头,以点结束。 我的python代码:

<s>

以下xml输出:

source_file = open("file.txt", "r")
for line in source_file:
    # catch ponctuation : . and , and ! and ? and ()
    if re.match("(\(|\)|\.|\,|\!)", str(line)):
        ET.SubElement(p, "pc").text = line
    else:
        ET.SubElement(p, "w").text = line

tree.write("my_file.xml", encoding="UTF-8", xml_declaration=True)

我面临的问题是我无法为每个新句子创建一个新的<?xml version="1.0" encoding="UTF-8"?> <p> <w>this</w> <w>is</w> <w>my</w> <w>first</w> <w>sentence</w> <pc>.</pc> <w>this</w> <w>is</w> <w>my</w> <w>second</w> <w>sentence</w> <pc>.</pc> </p> 标签,有没有办法使用python的xml库呢?

0 个答案:

没有答案