且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python gzip:有没有办法从字符串解压缩?

更新时间:2023-11-10 08:22:52

gzip.open是打开文件的简写,您想要的是gzip.GzipFile,您可以传递fileobj

gzip.open is a shorthand for opening a file, what you want is gzip.GzipFile which you can pass a fileobj

open(filename, mode='rb', compresslevel=9)
    #Shorthand for GzipFile(filename, mode, compresslevel).

vs

class GzipFile
   __init__(self, filename=None, mode=None, compresslevel=9, fileobj=None)
   #    At least one of fileobj and filename must be given a non-trivial value.

所以这应该对您有用

gzip_file_handle = gzip.GzipFile(fileobj=url_file_handle)