Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中遍历TEI时,某些实体的文本为空_Python_Xml_Elementtree_Tail_Tei - Fatal编程技术网

在Python3中遍历TEI时,某些实体的文本为空

在Python3中遍历TEI时,某些实体的文本为空,python,xml,elementtree,tail,tei,Python,Xml,Elementtree,Tail,Tei,我有一个TEI编码的xml文件,其实体如下: <sp> <speaker rend="italic">Sampson.</speaker> <ab> <lb n="5"/> <hi rend="italic">Gregory:</hi> <seg type="homograph">A</seg> my word wee'

我有一个TEI编码的xml文件,其实体如下:

<sp>
    <speaker rend="italic">Sampson.</speaker>
    <ab>
         <lb n="5"/>
         <hi rend="italic">Gregory:</hi>
         <seg type="homograph">A</seg> my word wee'l not carry coales.<lb n="6"/>
    </ab>
</sp>
<sp>
     <speaker rend="italic">Greg.</speaker>
     <ab>No, for then we should be Colliars.
         <lb n="7" rend="rj"/>
     </ab>
</sp>

输出很好地捕捉到了实体,但没有将“我的单词wee'l not carees”识别为第一个ab的文本。如果它在不同的元素中,我就看不到它。我曾考虑过将整个元素转换为字符串,并使用正则表达式(或剥离所有xml标记)获取元素文本,但我更愿意理解这里发生了什么。感谢您提供的帮助。

这是因为在
元素树
模型中,文本“我的话不会带煤。”被认为是
元素的
尾部,而不是
元素的
文本。要获取元素的文本及其子元素的尾部,可以尝试以下方法:

for i in doc.iter(tag='{http://www.tei-c.org/ns/1.0}ab'): 
    innerText = i.text+''.join((text.tail or '') for text in i.iter()).strip()  
    print(i.tag, innerText)
for i in doc.iter(tag='{http://www.tei-c.org/ns/1.0}ab'): 
    innerText = i.text+''.join((text.tail or '') for text in i.iter()).strip()  
    print(i.tag, innerText)