Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from '_io.BytesIO' to a bytes-like object in python3.6?

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?

like image 876
Dan Avatar asked Jan 10 '19 22:01

Dan


People also ask

Is BytesIO a file 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.

What does io BytesIO return?

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.

How do I save a BytesIO file in Python?

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.

What is byte stream in Python?

Byte streams are a sequence of bytes used by programs to input and output information.

How to convert string to bytes in Python?

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.

What is the use of stringio and bytesio in Python?

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.

How do you read bytes from an object in Python?

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.

How to solve the Python “TypeError: a bytes-like object is required”?

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.


3 Answers

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.

like image 115
wim Avatar answered Oct 18 '22 22:10

wim


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'
like image 52
Alexander Pacha Avatar answered Oct 18 '22 21:10

Alexander Pacha


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 ...
like image 3
Nwawel A Iroume Avatar answered Oct 18 '22 22:10

Nwawel A Iroume