Answer the question
In order to leave comments, you need to log in
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
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'
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question