This seems like it shouldn't be hard but I cannot find an example of doing this with a string, only with a file. I have a large string (3.5 mb). I want to loop over the string while reading n bytes per loop and perform an action on each chunk. What is the easiest way to do this? I'd prefer to do this with standard python libraries, not third party.
Asked
Active
Viewed 516 times
1
-
1To clarify, do you want to iterate over bytes or unicode codepoints? If the latter, then does this answer your question? [Iterate an iterator by chunks (of n) in Python?](https://stackoverflow.com/questions/8991506/iterate-an-iterator-by-chunks-of-n-in-python) – Brian61354270 Apr 19 '21 at 23:18
-
The code I have is in an aws lambda. It's a string being passed into the lambda. I need to process the string in 5000 byte chunks – johnsontroye Apr 19 '21 at 23:36
1 Answers
2
very_long_string = "very_long_string"
n = 3
for i in range(0, len(very_long_string), n):
substring = very_long_string[i:i+n]
# DO PROCESSING HERE

jsumskas
- 86
- 4
-
n represents number of chars? I would like to chunk the string on n = 5000 bytes or n = 5 kb. – johnsontroye Apr 19 '21 at 23:35
-
Yes, n is the number of chars. Assuming you are using ascii encoding, 1 char is one byte. – jsumskas Apr 19 '21 at 23:48