S
S
Sergey Ermakov2015-11-20 12:24:32
Python
Sergey Ermakov, 2015-11-20 12:24:32

How can I read a string byte by byte?

How can I read a string byte by byte in a loop?
For example, there is a parameter with a size of 100 bytes, I need to read it in 10 bytes.
My specific example is data in struct.pack (in the amount of 2 bytes and a total of 10 bytes of the necessary data), I need to read them in a loop.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abcd0x00, 2015-11-20
@abcd0x00

In general, you can use streams from io.

>>> import io
>>> 
>>> b = b'abcde' * 10
>>> 
>>> stream = io.BytesIO(b)
>>> while True:
...     block = stream.read(8)
...     if not block:
...         break
...     print(block)
... 
b'abcdeabc'
b'deabcdea'
b'bcdeabcd'
b'eabcdeab'
b'cdeabcde'
b'abcdeabc'
b'de'
>>>

But, judging by the description, simple cuts are needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question