2025-09-10:仓库迁移
This commit is contained in:
18
5.文件与IO/7.读写压缩的数据文件.py
Normal file
18
5.文件与IO/7.读写压缩的数据文件.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import gzip, bz2
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 如果想要读写.gz和.bz2后缀的压缩文件,请使用上述两个包
|
||||
with gzip.open("file.gz", 'rt') as f:
|
||||
text = f.read()
|
||||
|
||||
|
||||
with bz2.open("file.bz2", 'rt') as f:
|
||||
text2 = f.read()
|
||||
|
||||
# 同样的,如果写入可以使用wt和wb来进行,这些包里的open函数支持和open函数相同的操作
|
||||
# 如果想要增加压缩比,那就需要调整compresslevel参数,最高是默认9,调低它以获取更高的性能
|
||||
|
||||
# 这些函数还支持对二进制打开的压缩文件做类似的管道操作
|
||||
f = open("somefile.gz", 'rb')
|
||||
with gzip.open(f, 'rt') as g:
|
||||
text3 = g.read()
|
Reference in New Issue
Block a user