gpt4 book ai didi

python - 未封闭的 XML token

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:34 28 4
gpt4 key购买 nike

我将如何在 Python 2.6 中处理此错误?

Traceback (most recent call last):
File "./fetch_xml_collect.py", line 32, in <module>
tree=ET.parse(response)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse
tree.parse(source, parser)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 587, in parse
self._root = parser.close()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 1254, in close
self._parser.Parse("", 1) # end of data
xml.parsers.expat.ExpatError: unclosed token: line 56, column 1

当前正在实现的代码:

while urlget==1:
try:
response = urllib.urlopen(rep)
except IOError:
print("reason")
else:
try:
tree=ET.parse(response)
except IOError:
print("XML Parse Error\n")
else:
root=tree.getroot()
print root[0].text
powerlist=tree.findall('meter/power')
print powerlist[0].tag,powerlist[0].text

问题是:我将如何处理给定代码中的上述错误?

最佳答案

try:
#Some code
...
except xml.parsers.expat.ExpatError, ex:
print ex
continue

类似上面的东西应该可以工作。如果您收到该错误,只需继续。它将继续循环的下一次迭代,或者如果它是最后一次迭代,则跳出循环。

XML 格式不正确,无法处理。跳过它并继续下一个。

关于python - 未封闭的 XML token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15911674/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com