阅读7z档案中的巨大xml

时间:2019-03-04 16:56:47

标签: python python-3.x xml-parsing 7zip

我正在尝试在stackoverflow的数据转储中读取xml,但是我发现这并不是那么容易。这个想法是读取每个标签并提取一些东西。 我没有找到即时读取文件的方法。 这是我的代码(archive_path是一个包含文件名的7z文件)

import py7zlib
from xml.etree.cElementTree import iterparse

def test(archive_path,filename):
    with open(archive_path,'rb') as fp:
        archive = py7zlib.Archive7z(fp)
        context = iterparse(archive.getmember(filename), events=("start", "end"))
        context = iter(context)
        event, root = next(context)
        for event, elem in context:
            if event == "end" and elem.tag == "row":
                import code; code.interact(local=locals())
                root.clear()

(请注意我用于调试的导入代码) 这是指向随机小型stackexchange转储https://archive.org/download/stackexchange/webmasters.meta.stackexchange.com.7z

的链接

我现在面临的问题是iterparse似乎无法正常工作(python3)并且文件似乎无法正确读取(可以即时读取吗?)。

编辑:现在的代码为next(content)提供了一个例外

 File "stack.py", line 25, in readRows
   event, root = next(context) #context.__next__()
 File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 1223, in iterator
   data = source.read(16 * 1024)
TypeError: read() takes 1 positional argument but 2 were given

该错误可能是由于py7zlib的文件所致,该文件只有一个read()可以读取整个文件

0 个答案:

没有答案