python3解压缩函数 python压缩解压

python怎么解压压缩的字符串数据import StringIO
import gzip
compresseddata = https://www.04ip.com/post/gzip方式压缩python3解压缩函数的字符串(html)
compressedstream = StringIO.StringIO(compresseddata)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = https://www.04ip.com/post/gzipper.read() # data就是解压后python3解压缩函数的数据
一个简单python3解压缩函数的例子
1 import urllib2
2 from StringIO import StringIO
3 import gzip
4
5 def loadData(url):
6request = urllib2.Request(url)
7request.add_header('Accept-encoding', 'gzip')
8response = urllib2.urlopen(request)
9if response.info().get('Content-Encoding') == 'gzip':
10print 'gzip enabled'
11buf = StringIO(response.read())
12f = gzip.GzipFile(fileobj=buf)
13data = https://www.04ip.com/post/f.read()
14else:
15data = https://www.04ip.com/post/response.read()
16return data
用python解压图片并打印代码import zipfile
# 传入压缩文件zfile.zip获取相关信息
zip_file = zipfile.ZipFile('zfile.zip')
# 获取压缩文件中的内容
f_content = zip_file.namelist()
# 压缩前的大小
f_size = zip_file.getinfo('zfile/a.txt').file_size
# 压缩后的大小
c_size = zip_file.getinfo('zfile/a.txt').compress_size
ZipFile 对象有一个 namelist()方法,返回 ZIP 文件中包含的所有文件和文件夹 的字符串的列表 。这些字符串可以传递给 ZipFile 对象的 getinfo()方法,返回一个关 于特定文件的 ZipInfo 对象 。ZipInfo 对象有自己的属性,诸如表示字节数的 file_size 和 compress_size,它们分别表示原来文件大小和压缩后文件大小 。ZipFile 对象表示 整个归档文件,而 ZipInfo 对象则保存该归档文件中每个文件的有用信息 。
从 ZIP 文件中解压缩
ZipFile 对象的 extractall()方法从 ZIP 文件中解压缩所有文件和文件夹,放到当 前工作目录中 。
import zipfile
zip_file = zipfile.ZipFile('zfile.zip')
# 解压
zip_extract = zip_file.extractall()
zip_extract.close()
运行这段代码后,example.zip 的内容将被解压缩到 C:\ 。或者, 你可以向 extractall()传递的一个文件夹名称,它将文件解压缩到那个文件夹,而不是当前工作 目录 。如果传递给 extractall()方法的文件夹不存在,它会被创建 。例如,如果你用 exampleZip.extractall('C:\ delicious')取代?处的调用,代码就会从 example.zip 中解压 缩文件,放到新创建的 C:\delicious 文件夹中 。
ZipFile 对象的 extract()方法从 ZIP 文件中解压缩单个文件 。
创建和添加到 ZIP 文件
要创建你自己的压缩 ZIP 文件,必须以“写模式”打开 ZipFile 对象,即传入'w' 作为第二个参数(这类似于向 open()函数传入'w',以写模式打开一个文本文件) 。
如果向 ZipFile 对象的 write()方法传入一个路径,Python 就会压缩该路径所指 的文件,将它加到 ZIP 文件中 。write()方法的第一个参数是一个字符串,代表要添 加的文件名 。第二个参数是“压缩类型”参数 , 它告诉计算机使用怎样的算法来压 缩文件 。可以总是将这个值设置为 zipfile.ZIP_DEFLATED(这指定了 deflate 压缩 算法,它对各种类型的数据都很有效) 。
import zipfile
zip_file = zipfile.ZipFile('new.zip','w')
# 把zfile整个目录下所有内容,压缩为new.zip文件
zip_file.write('zfile',compress_type=zipfile.ZIP_DEFLATED)
# 把c.txt文件压缩成一个压缩文件
# zip_file.write('c.txt',compress_type=zipfile.ZIP_DEFLATED)
zip_file.close()
这段代码将创建一个新的 ZIP 文件,名为 new.zip,它包含 spam.txt 压缩后的内容 。
要记住,就像写入文件一样 , 写模式将擦除 ZIP 文件中所有原有的内容 。如果 只是希望将文件添加到原有的 ZIP 文件中,就要向 zipfile.ZipFile()传入'a'作为第二 个参数 , 以追加模式打开 ZIP 文件 。
请简述zip函数的功能及Python.3.x中使用zip函数生成列表的方法 。一、zip函数的功能如下:
将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表 。当各个迭代器中元素的个数不一致时,则返回列表中长度最短的情况,利用 *号操作符,可以将元组解压为列表 。
二、Python.3.x中使用zip函数生成列表的方法如下:
1、作出说明 , 使用zip函数将可迭代的对象作为参数 。
2、将对象中对应的元素打包成一个个元组 。
3、用zip函数平行地遍历多个迭代器,如果可迭代对象的长度不相同将按短的序列为准 。
4、遍历过程中产生元组,Python.3.x会把元组生成好,然后生成列表 。
如何通过Python压缩解压缩zip文件解压缩
import zipfile
import os
def un_zip(file_name):
"""unzip zip file"""
zip_file = zipfile.ZipFile(file_name)
if os.path.isdir(file_name"_files"):
pass
else:
os.mkdir(file_name"_files")
for names in zip_file.namelist():
zip_file.extract(names,file_name"_files/")
zip_file.close()
打包
zipfile.ZipFile('xxx.zip','a/w/x').write('xxx.txt')
'w'以截断并写入新文件'a'以附加到现有文件,或'x'以专门创建和写入新文件 。
python怎样压缩和解压缩ZIP文件使用 zipfile 模块进行 ZIP 压缩和解压缩
import zipfile
import os.path
import os
class ZFile(object):
def __init__(self, filename, mode='r', basedir=''):
self.filename = filename
self.mode = mode
if self.mode in ('w', 'a'):
self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED)
else:
self.zfile = zipfile.ZipFile(filename, self.mode)
self.basedir = basedir
if not self.basedir:
self.basedir = os.path.dirname(filename)
def addfile(self, path, arcname=None):
path = path.replace('//', '/')
if not arcname:
if path.startswith(self.basedir):
arcname = path[len(self.basedir):]
else:
arcname = ''
self.zfile.write(path, arcname)
def addfiles(self, paths):
for path in paths:
if isinstance(path, tuple):
self.addfile(*path)
else:
self.addfile(path)
def close(self):
self.zfile.close()
def extract_to(self, path):
for p in self.zfile.namelist():
self.extract(p, path)
def extract(self, filename, path):
if not filename.endswith('/'):
f = os.path.join(path, filename)
dir = os.path.dirname(f)
if not os.path.exists(dir):
os.makedirs(dir)
file(f, 'wb').write(self.zfile.read(filename))

def create(zfile, files):
z = ZFile(zfile, 'w')
z.addfiles(files)
z.close()
def extract(zfile, path):
z = ZFile(zfile)
z.extract_to(path)
z.close()
【python3解压缩函数 python压缩解压】python3解压缩函数的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于python压缩解压、python3解压缩函数的信息别忘了在本站进行查找喔 。

    推荐阅读