Cannot Split, A bytes-like object is required, not 'str'
Use decode()
correctly: either in two steps (if you want to reuse blah
):
blah = blah.decode()
splitblah = blah.split("\n")
# other code that uses blah
or inline (if you need it for single use):
splitblah = blah.decode().split("\n")
Your issue with using decode()
was that you did not use its return value. Note that decode()
does not change the object (blah
) to assign or pass it to something:
# WRONG!
blah.decode()
SEE ALSO:decode
docs.
If your question boils down to this:
I've tried using decode and encode but it still yells at me that the split method cannot use the datatype.
The error at hand can be demonstrated by the following code:
>>> blah = b'hello world' # the "bytes" produced by check_output
>>> blah.split('\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
In order to split bytes
, a bytes
object must also be provided. The fix is simply:
>>> blah.split(b'\n')
[b'hello world']