Last active
October 27, 2021 20:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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