Skip to content

Instantly share code, notes, and snippets.

@eioo
Last active October 27, 2021 20:59
Show Gist options
  • Save eioo/f9829bb14c35e65e832f4986b8a42683 to your computer and use it in GitHub Desktop.
Save eioo/f9829bb14c35e65e832f4986b8a42683 to your computer and use it in GitHub Desktop.
Split string into chunks that have max size and use new line as seperator
def split_at_newline(text, max_chars=4300):
rows = text.split('\n')
char_count = 0
messages = []
current_msg = ''
for text in rows:
new_char_count = char_count + len(text)
current_msg += text + '\n'
if new_char_count > max_chars:
messages.append(current_msg)
current_msg = ''
char_count = 0
continue
char_count = new_char_count
return messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment