XML ElementTree - 索引标签
XML ElementTree - indexing tags
我有一个 XML 文件:
<sentence id="en_BlueRibbonSushi_478218345:2">
<text>It has great sushi and even better service.</text>
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:3">
<text>The entire staff was extremely accomodating and tended to my every need.</text>
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:4">
<text>I've been to this restaurant over a dozen times with no complaints to date.</text>
</sentence>
使用 XML ElementTree,我想插入一个具有属性 category=
的标签 <Opinion>
。假设我有一个字符列表 list = ['a', 'b', 'c']
,是否可以将它们逐步分配给每个文本,所以我有:
<sentence id="en_BlueRibbonSushi_478218345:2">
<text>It has great sushi and even better service.</text>
<Opinion category='a' />
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:3">
<text>The entire staff was extremely accomodating and tended to my every need.</text>
<Opinion category='b' />
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:4">
<text>I've been to this restaurant over a dozen times with no complaints to date.</text>
<Opinion category='c' />
</sentence>
我知道我可以使用句子 id 属性,但这需要对我的代码进行大量重组。基本上,我希望能够索引每个句子条目以与我的列表索引对齐。
您可以使用 SubElement
工厂函数向树中添加元素。
假设您的 XML 数据在一个名为 data
的变量中,这会将元素添加到您的文档树中:
import xml.etree.ElementTree as ET
tree = ET.XML(data)
for elem, category in zip(tree.findall('sentence'), ['a', 'b', 'c']):
Opinion = ET.SubElement(elem, 'Opinion')
Opinion.set('category', category)
ET.dump(tree) # prints the tree; tree.write('output.xml') is another option
我有一个 XML 文件:
<sentence id="en_BlueRibbonSushi_478218345:2">
<text>It has great sushi and even better service.</text>
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:3">
<text>The entire staff was extremely accomodating and tended to my every need.</text>
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:4">
<text>I've been to this restaurant over a dozen times with no complaints to date.</text>
</sentence>
使用 XML ElementTree,我想插入一个具有属性 category=
的标签 <Opinion>
。假设我有一个字符列表 list = ['a', 'b', 'c']
,是否可以将它们逐步分配给每个文本,所以我有:
<sentence id="en_BlueRibbonSushi_478218345:2">
<text>It has great sushi and even better service.</text>
<Opinion category='a' />
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:3">
<text>The entire staff was extremely accomodating and tended to my every need.</text>
<Opinion category='b' />
</sentence>
<sentence id="en_BlueRibbonSushi_478218345:4">
<text>I've been to this restaurant over a dozen times with no complaints to date.</text>
<Opinion category='c' />
</sentence>
我知道我可以使用句子 id 属性,但这需要对我的代码进行大量重组。基本上,我希望能够索引每个句子条目以与我的列表索引对齐。
您可以使用 SubElement
工厂函数向树中添加元素。
假设您的 XML 数据在一个名为 data
的变量中,这会将元素添加到您的文档树中:
import xml.etree.ElementTree as ET
tree = ET.XML(data)
for elem, category in zip(tree.findall('sentence'), ['a', 'b', 'c']):
Opinion = ET.SubElement(elem, 'Opinion')
Opinion.set('category', category)
ET.dump(tree) # prints the tree; tree.write('output.xml') is another option