- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
由于没有人回答或评论这篇文章,我决定重写这篇文章。
考虑以下使用 lxml 的 Python 代码:
treeIter = etree.iterparse(fObj)
for event, ele in treeIter:
if ele.tag == 'logRoot':
try:
somefunction(ele)
except InternalException as e:
e.handle(*args)
ele.clear()
InternalException 是用户定义的,它包装了 somefunction() 除了 lxml.etree.XMLSyntaxError 之外的所有异常。 InternalException 具有明确定义的处理函数 .handle()。
fObj 有“trueRoot”作为顶级标签,许多“logRoot”作为二级叶子。
我的问题是:有没有办法在处理异常e时记录当前行号? *args 可以替换为任何可用的参数。
非常感谢任何建议。
最佳答案
import lxml.etree as ET
import io
def div(x):
return 1/x
content = '''\
<trueRoot>
<logRoot a1="x1"> 2 </logRoot>
<logRoot a1="x1"> 1 </logRoot>
<logRoot a1="x1"> 0 </logRoot>
</trueRoot>
'''
for event, elem in ET.iterparse(io.BytesIO(content), events=('end', ), tag='logRoot'):
num = int(elem.text)
print('Calling div({})'.format(num))
try:
div(num)
except ZeroDivisionError as e:
print('Ack! ZeroDivisionError on line {}'.format(elem.sourceline))
打印
Calling div(2)
Calling div(1)
Calling div(0)
Ack! ZeroDivisionError on line 4
关于python - lxml:调用etree.iterparse(f)时获取文件当前行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18192910/
我使用 iterparse 来解析一个大的 xml 文件 (1,8 GB)。我将所有数据写入 csv 文件。t 我制作的脚本运行良好,但由于某种原因它会随机跳行。这是我的脚本: import xml.
我正在使用 lxml iterparse 来读取巨大的 xml 文件。对于给定的 mainElement,我检查子元素并处理每个子元素。但我注意到,在检查元素中的子节点时,解析器有时实际上会丢失一些子
我想检索 XML 文件中特定元素的内容。然而,在 XML 元素中,还有其他 XML 元素,它们破坏了父标记中内容的正确提取。一个例子: xml = '''2. A protective uniform
我使用 Python 的 iterparse解析 nessus 扫描的 XML 结果(.nessus 文件)。对意外记录的解析失败,但相似的记录已被正确解析。 XML 文件的一般结构是很多记录,如下所
在回答另一个问题时,有人向我展示了以下教程,其中作者声称使用 iterparse 在 3 秒内解析了一个 ~100 MB 的 XML 文件: http://eli.thegreenplace.net/
有没有办法从lxml的lxml.etree.iterparse中得到多个标签名?我有一个类似文件的对象,它具有昂贵的读取操作和许多标签,因此获取所有标签或执行两次传递并不是最佳选择。 编辑:类似于 B
我正在尝试解析 xml。第一个 iterparse 工作正常,但第二个开始填充内存。如果删除第一个 iterparse,则什么都不会改变。Xml 有效。 def clear_element(e):
我正在尝试解析一个非常大的 XML 文件,因此我决定使用 lxml.iterparse,如所解释的 here . 所以我的代码如下所示: import sys from lxml import etr
我已经为此奋斗了一个小时。我正在用 iterparse 解析 XML 字符串.但是,数据没有正确编码,我不是它的提供者,所以我无法修复编码。 这是我得到的错误: lxml.etree.XMLSynta
如何解析大型 XML 文件并将其元素处理为 ObjectifiedElement(使用 objectify 解析器)。 我没有找到比以下更好的解决方案: from lxml import etree,
我想从文件(1.5gb 文件)中动态解析 xml,如下所示: x y URL__I_WANT_TO_PULLOUT
我不明白为什么会这样: content = urllib2.urlopen(url) context = etree.iterparse(content, tag='{my_ns}my_first_t
我有一个很大的 XML 文件,其中包含如下条目 (实际上,有多个 data 条目,它们在 XML 树中更深,但让我们保持简单。) 我想用 Python 读取这个文件并将所有
我需要整理一段代码,将一个可能很大的 XML 文件解析为自定义 Python 对象。思路大致如下: from lxml import etree for e, tag in etree.iterpar
我正在解析一个 700mb 的文件,我有以下代码在我的测试文件上运行良好,没有行 context.iter(context) 和 event, elem = context.next()。 form
过滤器是 using iterparse 解析一个简单的 XML StringIO object在unit test .但是,当之后尝试访问 StringIO 对象时,Python 会退出并显示“Va
import os import xml.etree.ElementTree as et for ev, el in et.iterparse(os.sys.stdin): el.clear(
我正在使用 elementtree.ElementTree.iterparse 来解析大型 (371 MB) xml 文件。 我的代码基本上是这样的: outf = open('out.txt', '
由于没有人回答或评论这篇文章,我决定重写这篇文章。 考虑以下使用 lxml 的 Python 代码: treeIter = etree.iterparse(fObj) for event, ele i
我想遍历 xml 文件的元素并生成每个元素,除非父元素是一个特征。 所以在伪代码中 for event, element in cElementTree.iterparse('../test.
我是一名优秀的程序员,十分优秀!