Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dechunking a string in python

Tags:

python

I have an array of bytes in python, which looks like this:

chunk_size = 8
a = b'12341234A12341234B12341234C12341234...'

I have to remove each nth byte (A, B, C in the example). Array length can be many thousands. Typical chunk size can be 128 or few thounds.

This is my current solution:

chunk_size = 8
output = b"".join([ i[:-1] for i in [ a[j:j+9] for j in range(0, len(a), chunk_size + 1) ]])

I am looking for other solutions, which could be more elegant.

Python 3.x only solutions are fine.

like image 279
Eriks Dobelis Avatar asked Apr 15 '26 16:04

Eriks Dobelis


2 Answers

I'd pull the 'chunk and skip' logic out into a generator, something like:

>>> a = b'12341234A12341234B12341234C12341234'
>>> def chunkify(buf, length):
...    while len(buf) > length:
...       # get the next chunk from the buffer
...       retval = buf[:length]
...       # ...and move along to the next interesting point in the buffer.
...       buf = buf[length+1:]
...       yield retval
...
>>> for chunk in chunkify(a, 8):
...    print chunk
...
12341234
12341234
12341234
>>> ''.join(chunk for chunk in chunkify(a, 8))
'123412341234123412341234'
like image 83
bgporter Avatar answered Apr 18 '26 06:04

bgporter


Something as simple as the following should do:

chunk_size = 8
a = b'12341234A12341234B12341234C12341234...'
b"".join(a[i:i+chunk_size] for i in range(0, len(a), chunk_size+1))

This works, because we process chuck_size+1 bytes in every iteration, but only keep chunk_size of them, effectively removing a byte after every chunk.

like image 22
jdoerrie Avatar answered Apr 18 '26 06:04

jdoerrie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!