Python 将文本拆分成几个部分

Python split text on several parts

我从一个函数返回了纯多行文本,我应该在 Telegram 或 Discord 中打印出来。问题是一条消息的字符限制。文本应该只用行分隔。例如

limit = 50

text = "Line1 - some text 
Line2  - some text
Line3 - some text, limit here
Line4 - some text"

我需要做一些事情才能得到

text1 = "Line1 - some text 
Line2  - some text"

text2 = "Line3 - some text, limit here
Line4 - some text"

或任何其他方式将长字符串分成几个部分,但只能按行。

这是错误的结果:

text1 = "Line1 - some text 
Line2  - some text 
Line3 - some"

text2 = "text, limit here
Line4 - some text"

一个简单的解决方案类似于

def send(x):
    #put your sending code here
    print(x)

s = "10\n1\n101\n10\n1" #example input

s= s.split("\n") # divides the string into lines
print(s)
#we want to send as many lines as possible without the total size of the sent string being over limit
limit = 3 #make this whatever you want
sending = ""
total = 0

for line in s:
    if total + len(line) > limit:
        send(sending[:-1])
        total = len(line)
        sending = line + "\n"
    else:
        total += len(line)
        sending += line + "\n"
#need to send the final string; there is probably a better way to do this, especially because this will break if the first if is entered on the last iteration
send(sending[:-1])

我怀疑有更好的方法可以在几行中使用一些巧妙的拆分或正则表达式来完成此操作,但这是一种将其逐行拆分为较小消息的粗暴方法。请注意,这将尝试发送超过字符限制的行,并且绝对可以改进。

将数据拆分到缓冲区的简单示例

import re

limit = 50
text = "Line1 - some text\nLine2  - some text\nLine3 - some text, limit here\nLine4 - some text"
tring_array=re.split('(\n)(\r\n)',text)

message=""
for current_str in string_array:
    if (len(message)+len(current_str)+1) <= limit:
        message+=(current_str+'\n')
    else:
        if len(message) == 0:
            print "buffer to smal or empty string"
            break
        else:
            print "Message: %sSize: %d" % (message,len(message))
            message=current_str+'\n'

if len(message)>0:
    print "Message: %sSize: %d" % (message,len(message))

结果

Message: Line1 - some text
Line2  - some text
Size: 37
Message: Line3 - some text, limit here
Line4 - some text
Size: 48