[英]printing child tags in xml file
I have an xml file with data that looks like this: 我有一个xml文件,其数据如下所示:
<SpeechSegment spkid="S0">
<Word dur="0.22" stime="0.44">oh</Word>
<Word dur="0.27" stime="1.67">bedankt</Word>
<Word dur="0.3" stime="2.03">voor</Word>
<Word dur="0.53" stime="2.61">deelname</Word>
</SpeechSegment>
What I want to do is count the words per segment and, if there are more than three words insert another "SpeechSegment" tag. 我想要做的是计算每段的单词,如果有超过三个单词,则插入另一个“SpeechSegment”标签。 So my preferred output is like this:
所以我的首选输出是这样的:
<SpeechSegment spkid="S0">
<Word dur="0.22" stime="0.44">oh</Word>
<Word dur="0.27" stime="1.67">bedankt</Word>
<Word dur="0.3" stime="2.03">voor</Word>
#count is more than 3
</SpeechSegment><SpeechSegment spkid="S0">
<Word dur="0.53" stime="2.61">deelname</Word>
</SpeechSegment>
I try to accomplish this using the following code: 我尝试使用以下代码完成此操作:
import xml.etree.ElementTree as ET
raw = ET.parse("Interview_short.xml")
root = raw.getroot()
for child in root:
print(child)
count_list = 0
for item in child:
print(item)
count_list = count_list + 1
if count_list > 2:
#add speech segment tag
I have the problem however that 不过我有问题
print(child)
gives me this: 给我这个:
<Element 'SpeechSegment' at 0x20e3cf8>.
While I am looking for 我正在寻找
<SpeechSegment spkid="S0">.
Adding .text after item does not work. 在项目后添加.text不起作用。 Any thoughts on what goes wrong here?
对这里出了什么问题的想法?
You can access the attributes of a tag by calling .attrib on the elements. 您可以通过在元素上调用.attrib来访问标记的属性。 In your case child.attrib will return the dictionary {'spkid': 'S0'} .
在你的情况下, child.attrib将返回字典{'spkid':'S0'} 。
Now you can access the keys and values in the dictionary in the normal way for python. 现在,您可以按照python的常规方式访问字典中的键和值。
child.attrib['spkid']
Hope that helps. 希望有所帮助。
If you were also asking how to add the new tags as well, please specify that in your question. 如果您还询问如何添加新标签,请在您的问题中指明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.