Decompressing a .bz2 file in Python

前端 未结 3 1698
野趣味
野趣味 2021-02-09 17:51

So, this is a seemingly simple question, but I\'m apparently very very dull. I have a little script that downloads all the .bz2 files from a webpage, but for some reason the dec

3条回答
  •  眼角桃花
    2021-02-09 18:12

    openZip = open(zipFile, "r")

    If you're running on Windows, you may want to do say openZip = open(zipFile, "rb") here since the file is likely to contain CR/LF combinations, and you don't want them to be translated.

    newLine = openZip.readline()

    As Alex pointed out, this is very wrong, as the concept of "lines" is foreign to a compressed stream.

    s = fileHandle.read(1024) [...] uncompressedData += bz2.decompress(s)

    This is wrong for the same reason. 1024-byte chunks aren't likely to mean much to the decompressor, since it's going to want to work with it's own block-size.

    s = fileHandle.read() uncompressedData = bz2.decompress(s)

    If that doesn't work, I'd say it's the new-line translation problem I mentioned above.

提交回复
热议问题