Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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
Python 为什么cElementTree iterparse不返回任何元素?_Python_Xml_Elementtree - Fatal编程技术网

Python 为什么cElementTree iterparse不返回任何元素?

Python 为什么cElementTree iterparse不返回任何元素?,python,xml,elementtree,Python,Xml,Elementtree,我正在尝试使用cElementTree.iterparse解析xml文件。 但是,我无法理解发生了什么,因为iterparse返回空元素。 我有一个xml文件,其大致布局如下: <DOCS> <ID id="1"> <HEAD>title1</HEAD> <DATE>21.01.2010</DATE> <TEXT> <P>some text</P>

我正在尝试使用cElementTree.iterparse解析xml文件。 但是,我无法理解发生了什么,因为iterparse返回空元素。 我有一个xml文件,其大致布局如下:

<DOCS>
  <ID id="1">
    <HEAD>title1</HEAD>
    <DATE>21.01.2010</DATE>
    <TEXT>
      <P>some text</P>
      <P>some text</P>
      <P>some text</P>
    </TEXT>
  </ID>

  <ID id="2">
    <HEAD>title2</HEAD>
    <DATE>21.01.2010</DATE>
    <TEXT>
      some text
    </TEXT>
  </ID>
</DATA>
当我执行代码时,我得到:

    docs[id] = ''.join([p.text for p in elem])
TypeError: sequence item 14: expected str instance, NoneType found

这意味着列表理解
[p.text for p in elem]
中的
p
之一是
None
。好的,我使用print语句来了解前面的
p
文本,以查看xml文件标记是否有问题。实际上,没有任何文本的
p
元素应该有它,因为它在xml文件中有一个文本体。有人能解释一下发生了什么吗?

如果事件=='end',忘记了
这个愚蠢的错误:
检查

因此,只有当
事件=='end'
时,我们才有一个完全填充的
elem
对象

    docs[id] = ''.join([p.text for p in elem])
TypeError: sequence item 14: expected str instance, NoneType found