gpt4 book ai didi

python - 从 ElementTree 获取更好的解析错误信息

转载 作者:数据小太阳 更新时间:2023-10-29 01:52:54 26 4
gpt4 key购买 nike

如果我尝试解析损坏的 XML,异常会显示行号。有没有办法显示 XML 上下文?

我想查看损坏部分前后的 xml 标记。

例子:

import xml.etree.ElementTree as ET
tree = ET.fromstring('<a><b></a>')

异常(exception):

Traceback (most recent call last):
File "tmp/foo.py", line 2, in <module>
tree = ET.fromstring('<a><b></a>')
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1300, in XML
parser.feed(text)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1642, in feed
self._raiseerror(v)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
raise err
xml.etree.ElementTree.ParseError: mismatched tag: line 1, column 8

像这样的东西会很好:

ParseError:
<a><b></a>
=====^

最佳答案

您可以创建一个辅助函数来执行此操作:

import sys
import io
import itertools as IT
import xml.etree.ElementTree as ET
PY2 = sys.version_info[0] == 2
StringIO = io.BytesIO if PY2 else io.StringIO

def myfromstring(content):
try:
tree = ET.fromstring(content)
except ET.ParseError as err:
lineno, column = err.position
line = next(IT.islice(StringIO(content), lineno))
caret = '{:=>{}}'.format('^', column)
err.msg = '{}\n{}\n{}'.format(err, line, caret)
raise
return tree

myfromstring('<a><b></a>')

产量

xml.etree.ElementTree.ParseError: mismatched tag: line 1, column 8
<a><b></a>
=======^

关于python - 从 ElementTree 获取更好的解析错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779375/

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