I want to split u"an arbitrary unicode string" into chunks of say 300 bytes without destroying any characters. The strings will be written to a socket that expects utf8 using unicode_string.encode("utf8"). I don’t want to destroy any characters. How would I do this?
I want to split uan arbitrary unicode string into chunks of say 300 bytes
Share
UTF-8 is designed for this.
Not tested. But you find a place to split, then backtrack until you reach the beginning of a character.
However, if a user might ever want to see an individual chunk, you may want to split on grapheme cluster boundaries instead. This is significantly more complicated, but not intractable. For example, in
"é", you might not want to split apart the"e"and the"´". Or you might not care, as long as they get stuck together again in the end.