1

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.

johnsontroye
  • 281
  • 1
  • 3
  • 20
  • 1
    To 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 Answers1

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