我试图读取stackoverflow数据转储中的xml,但我发现这并不是那么容易。其思想是读取每个标签并提取一些内容。 我没有找到一种方法来快速阅读文件。 这是我的代码(存档路径是一个包含文件名的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()来读取完整的文件
目前没有回答
相关问题 更多 >
编程相关推荐