Python 如何 trim 字节串

Python how to trim a bytes string

我想 trim 通过定位 $$$

找到的索引前的字节字符串
trimmed_bytes_stream = padded_bytes_stream[:padded_stream.index('$$$')]

但出现错误:

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

是否有字节对象等效方法可以做到这一点?或者将字节字符串转换为字符串然后使用字符串方法?在 trimming?

之后最终转换回字节

b附加到您的搜索项

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