- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何解析大型 XML 文件并将其元素处理为 ObjectifiedElement(使用 objectify 解析器)。
我没有找到比以下更好的解决方案:
from lxml import etree, objectify
for event, elt in etree.iterparse('onebigfile.xml', tag='MyTag'):
oelt = objectify.fromstring(etree.tostring(elt))
my_process(oelt)
如何避免这种中间字符串表示形式?
最佳答案
我觉得真的很好用iterparse
构建自定义数据提取器,完全无需使用 objectify。
为了这个示例,我使用了一个看起来有点像这样的 .NET 引用 XML 文件:
<doc>
<assembly>
<name>System.IO</name>
</assembly>
<members>
<member name="T:System.IO.BinaryReader">
<summary>Reads primitive data types as binary values in a specific encoding.</summary>
<filterpriority>2</filterpriority>
</member>
<member name="M:System.IO.BinaryReader.#ctor(System.IO.Stream)">
<summary>Initializes a new instance of the <see cref="T:System.IO.BinaryReader" /> class based on the specified stream and using UTF-8 encoding.</summary>
<param name="input">The input stream. </param>
<exception cref="T:System.ArgumentException">The stream does not support reading, is null, or is already closed. </exception>
</member>
<member name="M:System.IO.BinaryReader.#ctor(System.IO.Stream,System.Text.Encoding)">
<summary>Initializes a new instance of the <see cref="T:System.IO.BinaryReader" /> class based on the specified stream and character encoding.</summary>
<param name="input">The input stream. </param>
<param name="encoding">The character encoding to use. </param>
<exception cref="T:System.ArgumentException">The stream does not support reading, is null, or is already closed. </exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="encoding" /> is null. </exception>
</member>
<!-- ... many more members like this -->
</members>
</doc>
假设您想要将所有成员及其名称、摘要和属性提取为像这样的字典列表:
{
'summary': 'Reads primitive data types as binary values in a specific encoding.',
'name': 'T:System.IO.BinaryReader'
}
{
'summary': 'Initializes a new instance of the ',
'@input': 'The input stream. ',
'name': 'M:System.IO.BinaryReader.#ctor(System.IO.Stream)'
}
{
'summary': 'Initializes a new instance of the class based on the specified stream and using UTF-8 encoding.',
'@input': 'The input stream. ',
'@encoding': 'The character encoding to use. ',
'name': 'M:System.IO.BinaryReader.#ctor(System.IO.Stream,System.Text.Encoding)'
}
你可以这样做:
lxml.iterparse
与 start
和 end
事件<member>
元素开始,准备一个新的字典(item
)<member>
元素,将我们感兴趣的任何内容添加到字典中<member>
元素结束,完成字典并产生它item
至 None
用作“<member>
的内部/外部”-flag 在代码中:
import lxml
from lxml import etree
def text_content(elt):
return ' '.join([t.strip() for t in elt.itertext()])
def extract_data(xmlfile):
item = None
for event, elt in etree.iterparse(xmlfile, events=['start', 'end']):
if elt.tag == 'member':
if event == 'start':
item = {}
else:
item['name'] = elt.attrib['name']
yield item
item = None
if item == None:
continue
if event == 'end':
if elt.tag in ('summary', 'returns'):
item[elt.tag] = text_content(elt)
continue
if elt.tag == 'param':
item['@' + elt.attrib['name']] = text_content(elt)
continue
testfile = r'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.IO.xml'
for item in extract_data(testfile):
print(item)
通过这种方式,您可以获得最快和最节省内存的解析,并可以很好地控制您查看的数据。使用 objectify
会比没有中间体更浪费 tostring()
/fromstring()
.
关于python - lxml iterparse 与 objectify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49880545/
我使用 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.
我是一名优秀的程序员,十分优秀!