I am using this function to uncompress the body or a HTTP response if it is compressed with gzip, compress or deflate.
def uncompress_body(self, compression_type, body):
if compression_type == 'gzip' or compression_type == 'compress':
return zlib.decompress(body)
elif compression_type == 'deflate':
compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
compressed = compressor.compress(body)
compressed += compressor.flush()
return base64.b64encode(compressed)
return body
However python throws this error message.
TypeError: a bytes-like object is required, not '_io.BytesIO'
on this line:
return zlib.decompress(body)
Essentially, how do I convert from '_io.BytesIO' to a bytes-like object?
StringIO and BytesIO are methods that manipulate string and bytes data in memory. StringIO is used for string data and BytesIO is used for binary data. This classes create file like object that operate on string data. The StringIO and BytesIO classes are most useful in scenarios where you need to mimic a normal file.
It takes input POSIX based arguments and returns a file descriptor which represents the opened file. It does not return a file object; the returned value will not have read() or write() functions.
First, open a file in binary write mode and then specify the contents to write in the form of bytes. Next, use the write function to write the byte contents to a binary file.
Byte streams are a sequence of bytes used by programs to input and output information.
no arguments : Returns array of size 0. In this example, we are going to convert string to bytes using the Python bytes () function, for this we take a variable with string and pass it into the bytes () function with UTF-8 parameters.
Python: Using StringIO and BytesIO for managing data as file object. Using buffer modules(StringIO, BytesIO, cStringIO) we can impersonate string or bytes data like a file.These buffer modules help us to mimic our data like a normal file which we can further use for processing.
The read method can be used to read the bytes from an object and return them. The method takes an optional size argument if you want to specify up to how many bytes to return. If the argument is not provided, all bytes until the end of the file are returned.
To solve the Python "TypeError: a bytes-like object is required, not _io.BytesIO", call the read () method on the _io.BytesIO object, e.g. b.read (). The read method reads the bytes from the object and returns them. Here is an example of how the error occurs. We tried using an _io.BytesIO object where a bytes object is expected.
It's a file-like object. Read them:
>>> b = io.BytesIO(b'hello')
>>> b.read()
b'hello'
If the data coming in from body
is too large to read into memory, you'll want to refactor your code and use zlib.decompressobj
instead of zlib.decompress
.
In case you write into the object first, make sure to reset the stream before reading:
>>> b = io.BytesIO()
>>> image = PIL.Image.open(path_to_image)
>>> image.save(b, format='PNG')
>>> b.seek(0)
>>> b.read()
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x06\xcf\x00\x00\x03W\x08\x02\x00'
or directly get the data with getvalue
>>> b.getvalue()
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x06\xcf\x00\x00\x03W\x08\x02\x00'
b = io.BytesIO()
image = PIL.Image.open(path_to_image) # ie 'image.png'
image.save(b, format='PNG')
b.getbuffer().tobytes() # b'\x89PNG\r\n\x1a\n\x00 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With