Extract zip to memory, parse contents

前端 未结 4 866
孤独总比滥情好
孤独总比滥情好 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

    Don't overthink it. It Just Works:

    import zipfile
    
    # 1) I want to read the contents of a zip file ...
    with zipfile.ZipFile('A-Zip-File.zip') as zipper:
      # 2) ... find a particular file in the archive, open the file ...
      with zipper.open('A-Particular-File.txt') as fp:
        # 3) ... and extract a line from it.
        first_line = fp.readline()
    
    print first_line
    

提交回复
热议问题