如何在Python中gzip字节数组? [英] How to gzip a bytearray in Python?
本文介绍了如何在Python中gzip字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个字节数组中有二进制数据,我想先gzip然后通过请求发布.我发现了如何gzip文件,但找不到字节数组.那么,如何通过Python gzip一个字节数组?
I have binary data inside a bytearray that I would like to gzip first and then post via requests. I found out how to gzip a file but couldn't find it out for a bytearray. So, how can I gzip a bytearray via Python?
推荐答案
看看Python的zlib
-模块.
Have a look at the zlib
-module of Python.
Python 3: zlib
-模块
Python 3: zlib
-module
一个简短的例子:
import zlib
compressed_data = zlib.compress(my_bytearray)
您可以通过以下方式再次解压缩数据:
You can decompress the data again by:
decompressed_byte_data = zlib.decompress(compressed_data)
Python 2: zlib
-模块
Python 2: zlib
-module
一个简短的例子:
import zlib
compressed_data = zlib.compress(my_string)
您可以通过以下方式再次解压缩数据:
You can decompress the data again by:
decompressed_string = zlib.decompress(compressed_data)
如您所见,Python 3使用字节数组,而Python 2使用字符串.
As you can see, Python 3 uses bytearrays while Python 2 uses strings.
这篇关于如何在Python中gzip字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文