python gzip_Python之gzip模块的使用

gzip模块作用:

为GNU zip文件提供了一个类似的接口,它使用zlib来压缩和解压数据。

1、写压缩gzip文件

#!/usr/bin/env python3#encoding: utf-8

importgzipimportioimportos

out_file_name= "example.text.gz"with gzip.open(out_file_name,‘wb‘) as output:

with io.TextIOWrapper(output, encoding=‘utf-8‘) as enc:

enc.write(‘test gzip content‘)print(out_file_name, ‘包含的大小:{}bytes‘.format(os.stat(out_file_name).st_size))

os.system(‘file -b --mime {}‘.format(out_file_name))

gzip_write.py

测试效果

[[email protected] mnt]# python3 gzip_write.py

example.text.gz 包含的大小:56bytes

application/x-gzip; charset=binary

[[email protected] mnt]# ll

total12

-rw-r--r-- 1 root root 56 Jan 1 09:16example.text.gz-rw-r--r-- 1 root root 464 Jan 1 09:13 gzip_write.py

2、gzip压缩级别的测试以及适当设置值

#!/usr/bin/env python3#encoding: utf-8

importgzipimportioimportosimporthashlibdefget_hash(data):"""返回md5值"""

returnhashlib.md5(data).hexdigest()#读取文件内容,并且复制1024份出来

data = open(‘content.txt‘, ‘r‘).read() * 1024

#输入文件内容,返回md5值

check_sum = get_hash(data.encode(‘utf-8‘))print(‘Level Size Checksum‘)print(‘----- ---------- ---------------------------------‘)print(‘data {:>5} {}‘.format(len(data), check_sum))for i in range(0, 10):

file_name= ‘compress-level-{}.gz‘.format(i)

with gzip.open(file_name,‘wb‘, compresslevel=i) as output:

with io.TextIOWrapper(output, encoding=‘utf-8‘) as enc:

enc.write(data)

size=os.stat(file_name).st_size

check_sum= get_hash(open(file_name, ‘rb‘).read())print(‘{} {:>4} {}‘.format(i, size, check_sum))

gzip_compresslevel.py

运行效果

[[email protected] mnt]#python3 gzip_compresslevel.py

Level Size Checksum----- ---------- ---------------------------------data34406403456cbea4852fa964775f38f03f2f2b

03441591b9895c8eaa94a0fdc5002d35fd44a931 333342f88491459c7a7028da1ffb5f2bdc822 311413771e090b90d4692a710798562561913 3114f822f153e8b6da768f8b84559e75e2ca4 1797 41e03538d99b3697db87901537e2577f #4以后最优

5 17976cf8fcb66c90ae9a15e480db852800b46 179738064eed4cad2151a6c33e6c6a18c7ec7 1797dab0bd23a4d856da383cda3caea87a828 1797782dc69ce1d62e4646759790991fb5319 1797 6d2d8b1532d1a2e50d7e908750aad446

3、gzip多行的写入压缩

#!/usr/bin/env python3#encoding: utf-8

importgzipimportioimportitertools

with gzip.open(‘example_line.txt.gz‘,‘wb‘) as output:

with io.TextIOWrapper(output,encoding=‘utf-8‘) as enc:

enc.writelines(

itertools.repeat(‘The same line\n‘,10) #重复写入10次

)

gzip_writelines.py

运行效果

[[email protected] mnt]# python3 gzip_writelines.py

[[email protected] mnt]# ll

total12

-rw-r--r-- 1 root root 60 Jan 1 09:41example_line.txt.gz-rw-r--r-- 1 root root 369 Jan 1 09:40gzip_writelines.py

[[email protected] mnt]#gzip -d example_line.txt.gz

[[email protected] mnt]# ll

total12

-rw-r--r-- 1 root root 140 Jan 1 09:41example_line.txt-rw-r--r-- 1 root root 369 Jan 1 09:40gzip_writelines.py

[[email protected] mnt]#catexample_line.txt

The same line

The same line

The same line

The same line

The same line

The same line

The same line

The same line

The same line

The same line

4、读取gzip压缩文件内容

#!/usr/bin/env python3#encoding: utf-8

importgzipimportio

with gzip.open(‘example.text.gz‘, ‘rb‘) as input_file:

with io.TextIOWrapper(input_file, encoding=‘utf-8‘) as dec:print(dec.read())

gzip_read.py

运行效果

[[email protected] mnt]# python3 gzip_read.py

testgzip content

5、读取gzip压缩文件利用seek定位取值

#!/usr/bin/env python3#encoding: utf-8

importgzipimportio

with gzip.open(‘example.text.gz‘, ‘rb‘) as input_file:print(‘读取整个压缩文件的内容‘)

all_data=input_file.read()print(all_data)

expected= all_data[5:10] #切片取值

print(‘切片取值:‘, expected)#将流文件的指针切至起始点

input_file.seek(0)#将流文件的指针切至5的下标

input_file.seek(5)

new_data= input_file.read(5)print(‘移动指针取值:‘, new_data)print(‘判断两种取值是否一样:‘,expected == new_data)

gzip_seek.py

运行效果

[[email protected] mnt]# python3 gzip_seek.py

读取整个压缩文件的内容

b‘test gzip content‘切片取值: b‘gzip‘移动指针取值: b‘gzip‘判断两种取值是否一样: True

6、gzip字节流的处理(ByteIO)的示例

#!/usr/bin/env python3#encoding: utf-8

importgzipfrom io importBytesIOimportbinascii#获取未压缩的数据

uncompress_data = b‘The same line,over and over.\n‘ * 10

print(‘Uncompressed Len:‘, len(uncompress_data))print(‘Uncompress Data:‘, uncompress_data)

buffer=BytesIO()

with gzip.GzipFile(mode=‘wb‘, fileobj=buffer) as f:

f.write(uncompress_data)#获取压缩的数据

compress_data =buffer.getvalue()print(‘Compressed:‘, len(compress_data))print(‘Compress Data:‘, binascii.hexlify(compress_data))#重新读取数据

inbuffer =BytesIO(compress_data)

with gzip.GzipFile(mode=‘rb‘, fileobj=inbuffer) as f:

reread_data=f.read(len(uncompress_data))print(‘利用未压缩的长度,获取压缩后的数据长度:‘, len(reread_data))print(reread_data)

gzip_BytesIO.py

[[email protected] mnt]# python3 gzip_BytesIO.py

Uncompressed Len:290Uncompress Data: b‘The same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\n‘Compressed:51Compress Data: b‘1f8b0800264c0c5e02ff0bc94855284ecc4d55c8c9cc4bd5c92f4b2d5248cc4b510031f4b8424625f5b8008147920222010000‘利用未压缩的长度,获取压缩后的数据长度:290b‘The same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\nThe same line,over and over.\n‘

原文:https://www.cnblogs.com/ygbh/p/12128233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值