【问题标题】:How to iterparse a zipfile object如何迭代解析 zipfile 对象
【发布时间】:2017-03-24 16:16:04
【问题描述】:

如何将 zipfile 对象转换为可用于 iterparse 函数的对象?

for name in zipfile.namelist():
xml_zip = zipfile.open(name, 'r')

for bla, elem in etree.iterparse(xml_zip):
    print bla

我尝试将带有 .read() 的 zipfile 对象转换为字符串。但这会带来问题,因为它不是 Unicode。最好的方法是什么?

【问题讨论】:

    标签: python xml parsing zip elementtree


    【解决方案1】:

    您应该发布一个完整的示例,您发布的代码 sn-p 不起作用,iterparse 不是 etree 模块的属性。这行得通:

    import zipfile
    from xml.etree import ElementTree
    
    with zipfile.ZipFile('zipfile.zip') as z:
        for fname in z.namelist():
            with z.open(fname) as f:
                for event, elem in ElementTree.iterparse(f):
                    print elem
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2022-09-30
      • 1970-01-01
      相关资源
      最近更新 更多