将zip解压缩到内存中,解析内容 [英] Extract zip to memory, parse contents

查看:92
本文介绍了将zip解压缩到内存中,解析内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将zip文件的内容读取到内存中,而不是将其提取到光盘中,在存档中找到特定文件,打开文件并从中提取一行.

I want to read the contents of a zip file into memory rather than extracting them to disc, find a particular file in the archive, open the file and extract a line from it.

可以打开并解析StringIO实例吗?有什么建议吗?预先感谢.

Can a StringIO instance be opened and parsed? Suggestions? Thanks in advance.

zfile = ZipFile('name.zip', 'r')

    for name in zfile.namelist():
        if fnmatch.fnmatch(name, '*_readme.xml'):
            name = StringIO.StringIO()
            print name # prints StringIO instances
            open(name, 'r')  # IO Error: No such file or directory...

我发现了一些类似的帖子,但似乎都没有解决这个问题:提取zipfile记忆?

I found a few similar posts, but none that seem to address this issue: Extracting a zipfile to memory?

推荐答案

感谢所有提供解决方案的人.这就是对我有用的东西:

Thank you to everyone that contributed solutions. This is what ended up working for me:

zfile = ZipFile('name.zip', 'r')

        for name in zfile.namelist():
            if fnmatch.fnmatch(name, '*_readme.xml'):
                zopen = zfile.open(name)
                for line in zopen:
                    if re.match('(.*)<foo>(.*)</foo>(.*)', line):
                        print line

这篇关于将zip解压缩到内存中,解析内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆