Reputation: 185
In Python, you can do:
b = bytearray(100)
b[0:3] = 'one'.encode()
b[17:20] = 'two'.encode()
This, however, creates an intermediate bytes() object, which is leading to suboptimal performance.
Is there anything like encode_into() that will encode a string directly into a bytearray?
Upvotes: 2
Views: 78
Reputation: 309929
I assume you're working in python3.x, otherwise b[0:3] = 'one'
works just fine.
For python3.x, you can use the b
string prefix:
b[0:3] = b'one' # parser creates a bytes object, not a string.
Upvotes: 3