简体   繁体   English

在一定数量的字符后拆分字符串

[英]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谢谢

Does this give you the result you're looking for?这会给您想要的结果吗? If not, could you please provide an example string with expected output?如果不是,您能否提供一个预期为 output 的示例字符串?

import re
CHARACTER_LIMIT = 2048

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

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'
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM