Lysander12 PT
Lysander12 PT

Reputation: 164

Spliting string after certain amount of characters

I have a lengthy string and want to split it after a certain number of characters. I already have done this:

if len(song.lyrics) > 2048:
    string1 = string[:2048]
    string2 = string[2049:]

The problem with this is that sometimes it breaks in the middle of text and I don't want to. Is there a way to get the last linebreak before the character limit is reached and break it there? Thanks

Upvotes: 0

Views: 410

Answers (2)

wwii
wwii

Reputation: 23743

Find the index of newline character just-left-of your length limit then use it to split.

if len(song.lyrics) > 2048:
    index = string[:2048].rfind('\n')
    string1 = string[:index]
    string2 = string[index+1:]

Example:

>>> s = 'aaaaaaa\nbbbbbbbbbbbbbbbb\nccccccc\ndddddddddddddddd'
>>> limit = 31   #                        ↑
>>> index = s[:limit].rfind('\n')
>>> index
24
>>> s1,s2 = s[:index],s[index+1:]
>>> s1
'aaaaaaa\nbbbbbbbbbbbbbbbb'
>>> s2
'ccccccc\ndddddddddddddddd'
>>>

Upvotes: 0

kerasbaz
kerasbaz

Reputation: 1794

Does this give you the result you're looking for? If not, could you please provide an example string with expected output?

import re
CHARACTER_LIMIT = 2048

for m in re.finditer(r'.{,%s}(?:\n|$)' % CHARACTER_LIMIT, string, re.DOTALL):
    print(m.group(0))

Upvotes: 2

Related Questions