我有一个脚本,可以从URL列表中提取XML文件中的一些术语。 所有URL都可以访问XML数据。
它首先正常打开,解析和提取工作正常,但在此过程中被一些XML文件中断并出现此错误:
File "<stdin>", line 18, in <module>
File "lxml.etree.pyx", line 2953, in lxml.etree.parse (src/lxml/lxml.etree.c:56204)
File "parser.pxi", line 1555, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82511)
File "parser.pxi", line 1585, in lxml.etree._parseFilelikeDocument (src/lxml/lxml.etree.c:82832)
File "parser.pxi", line 1468, in lxml.etree._parseDocFromFilelike (src/lxml/lxml.etree.c:81688)
File "parser.pxi", line 1024, in lxml.etree._BaseParser._parseDocFromFilelike (src/lxml/lxml.etree.c:78735)
File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74472)
File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75363)
File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74696)
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
从我的搜索中可能是因为某些XML文件有空格,但我不确定它是否是问题。我无法分辨哪些文件会出错。 有没有办法解决这个错误?
这是我的剧本:
URLlist = ["http://www.uniprot.org/uniprot/"+x+".xml" for x in IDlist]
for id, item in zip(IDlist, URLlist):
goterm_location = []
goterm_function = []
goterm_process = []
location_list[id] = []
function_list[id] = []
biological_list[id] = []
try:
textfile = urllib2.urlopen(item);
except urllib2.HTTPError:
print("URL", item, "could not be read.")
continue
#Try to solve empty line error#
tree = etree.parse(textfile);
#root = tree.getroot()
for node in tree.iter('{http://uniprot.org/uniprot}dbReference'):
if node.attrib.get('type') == 'GO':
for child in node:
value = child.attrib.get('value');
if value.startswith('C:'):
goterm_C = node.attrib.get('id')
if goterm_C:
location_list[id].append(goterm_C);
if value.startswith('F:'):
goterm_F = node.attrib.get('id')
if goterm_F:
function_list[id].append(goterm_F);
if value.startswith('P:'):
goterm_P = node.attrib.get('id')
if goterm_P:
biological_list[id].append(goterm_P);
我试过了:
tree = etree.iterparse(textfile, events = ("start","end"));
OR
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(textfile, parser)
没有成功。 任何帮助将不胜感激
答案 0 :(得分:3)
我无法判断哪些文件会出错
通过在解析之前打印文件/ URL的名称进行调试。然后,您将看到哪个文件导致错误。
另外,请阅读错误消息:
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
这表明下载的XML文件为空。确定导致问题的URL后,请尝试下载文件并检查其内容。我怀疑它可能是空的。
在解析时,您可以通过使用try / except块来忽略有问题的文件(空或语法无效):
try:
tree = etree.parse(textfile)
except lxml.etree.XMLSyntaxError:
print 'Skipping invalid XML from URL {}'.format(item)
continue # go on to the next URL
或者您可以通过查看内容长度&#39;来检查空文件。标题,或甚至通过读取urlopen()
返回的资源,但我认为上述情况更好,因为它还会捕获其他潜在的错误。
答案 1 :(得分:0)
我在 Python 3.6 中收到相同的错误消息
lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
就我而言,xml 文件不为空。问题是因为编码,
最初使用utf-8,
from lxml import etree
etree.iterparse(my_xml_file.xml, tag='MyTag', encoding='utf-8')
将编码更改为 iso-8859-1 解决了我的问题,
etree.iterparse(my_xml_file.xml, tag='MyTag', encoding='iso-8859-1')