Extract zip to memory, parse contents

前端 未结 4 845
孤独总比滥情好
孤独总比滥情好 2021-01-22 21:30

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.

4条回答
  •  甜味超标
    2021-01-22 22:11

    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('(.*)(.*)(.*)', line):
                            print line
    

提交回复
热议问题