How do i convert from '_io.BytesIO' to a bytes-like object in python3.6?

Dan :

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?

Thanks

wim :

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.

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

csv file convert to io.BytesIO object, then stream to blob storage ,meets value type error:a bytes-like object is required, not '_io.TextIOWrapper'

Why I have to convert bytes from BytesIO then convert back to BytesIO so it can be read as PDF file response?

How do I convert a bytes object to a string in python?

How do I convert Pandas object to a list in python3

How do I convert a string into bytes in python?

How do I convert a list of ASCII "Bytes" in Python into Hex Bytes

How can I convert string to bytes in Python, like node js

Python3 How to make a bytes object from a list of integers

How do I convert line 6 and 7 from python into javascript?

TypeError: a bytes-like object is required, not 'str' Using BytesIO

how do i convert my supervisord configuration from python2 to python3?

How can I convert bytes object to decimal or binary representation in python?

BytesIO like file object

How do I convert a string into a string of bytes in python 2.7

Convert file into BytesIO object using python

How to convert bytes from Kafka to their original object?

TypeError: a bytes-like object is required, not 'str' how do I tackle this?

Tweepy, a bytes-like object is required, not str. How do i fix this error?

How do I convert lists and dictionaries into bytes?

How do I convert a list of integers into bytes?

How do I convert a B string to bytes?

python3: TypeError: a bytes-like object is required, not 'str'

Python3 TypeError: a bytes-like object is required, not 'str'

TypeError: a bytes-like object is required, not 'int' python3

How do I create a Python bytes object in the C API

How do I convert a python object to (json) nested dict using the json module, without making a file-like object?

How do I format this from str to byte-like object?

Python convert xml to json a bytes-like object is required