我有一个名为“interview.xml”的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>
<Word dur="0.22" stime="3.15">aan</Word>
<Word dur="0.23" stime="3.39">de</Word>
<Word dur="0.14" stime="6.15">want</Word>
<Word dur="0.07" stime="6.29">ik</Word>
<Word dur="0.09" stime="6.36">wil</Word>
<Word dur="0.06" stime="6.45">je</Word>
<Word dur="0.42" stime="6.51">graag</Word>
<Word dur="0.2" stime="7.52">en</Word>
</SpeechSegment>
我现在要做的是解析这个片段中的所有单词,所以我想创建一个类似["oh", "bedankt", "voor", etc...]
的列表
我试过这个:
import xml.etree.ElementTree
e = xml.etree.ElementTree.parse('Interview_short.xml').getroot()
for atype in e.findall('type'):
print(atype.get('word'))
但这并没有给我想要的结果。你觉得我应该做些什么调整
使用^{}
解决方案:
我不知道为什么
findall('type')
而XML不包含任何<type>
元素。根据发布的XML,它应该是findall('Word')
。下面是一个最小但完整的演示代码:^{}
输出:
相关问题 更多 >
编程相关推荐