Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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 ElementTree解析xml时出现非类型异常_Python_Xml_Elementtree_Nonetype - Fatal编程技术网

Python ElementTree解析xml时出现非类型异常

Python ElementTree解析xml时出现非类型异常,python,xml,elementtree,nonetype,Python,Xml,Elementtree,Nonetype,我遇到了一个非常糟糕的问题。这是代码 #!/usr/bin/python # filename: parse_dblp.py # author: ivanchou import codecs, os import xml.etree.ElementTree as ET paper_tag = ('article','inproceedings','proceedings','book', 'incollection','phdthesis','mastersthesi

我遇到了一个非常糟糕的问题。这是代码

#!/usr/bin/python
# filename: parse_dblp.py
# author: ivanchou

import codecs, os
import xml.etree.ElementTree as ET
paper_tag = ('article','inproceedings','proceedings','book',
             'incollection','phdthesis','mastersthesis','www')

class AllEntities:
    def __getitem__(self, key):
        return key


print ('----------parse begin----------')

# the parse result store to authors
result = codecs.open('authors','w','utf-8')

parser = ET.XMLParser()
parser.parser.UseForeignDTD(True)
parser.entity = AllEntities()

for event, article in ET.iterparse('dblp_part.xml', events=("start", 

"end"), parser=parser):
    for author in article.findall('author'):
        result.write(author.text + u'|')
    if event == 'end' and article.tag in paper_tag:
        result.write(os.linesep)
    article.clear()
print ('----------parse end----------')
我在这里创建的文件dblp_part.xml有一个要点:

它包含dblp.xml的head 2336行,最后一个article元素返回NoneType error,如果我交换最后两个元素,一切正常。这是ElementTree的一个bug吗


我是python新手,搜索帮助。

dblp_part.xml有一些不合适的地方xml@Leon这是正确的xml