Search code examples
python-3.xtrim

Python how to trim a bytes string


I want to trim a bytes string before an index found by locating $$$,

trimmed_bytes_stream = padded_bytes_stream[:padded_stream.index('$$$')]

but got an error:

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

Is there bytes object equivalent methods to do that? Or have convert bytes string to string and then using string methods? finally convert back to bytes after trimming?


Solution

  • Append a b to your search item

    trimmed_bytes_stream = padded_bytes_stream[:padded_stream.index(b'$$$')]