Dechunking a string in python -
i have array of bytes in python, looks this:
chunk_size = 8 = b'12341234a12341234b12341234c12341234...'
i have remove each nth byte (a, b, c in example). array length can many thousands. typical chunk size can 128 or few thounds.
this current solution:
chunk_size = 8 output = b"".join([ i[:-1] in [ a[j:j+9] j in range(0, len(a), chunk_size + 1) ]])
i looking other solutions, more elegant.
python 3.x solutions fine.
i'd pull 'chunk , skip' logic out generator, like:
>>> = b'12341234a12341234b12341234c12341234' >>> def chunkify(buf, length): ... while len(buf) > length: ... # next chunk buffer ... retval = buf[:length] ... # ...and move along next interesting point in buffer. ... buf = buf[length+1:] ... yield retval ... >>> chunk in chunkify(a, 8): ... print chunk ... 12341234 12341234 12341234 >>> ''.join(chunk chunk in chunkify(a, 8)) '123412341234123412341234'
Comments
Post a Comment