拆分短信回复

时间:2010-06-18 23:05:40

标签: python sms

回复短信时,我有160个字符的限制。我目前设置代码以进行回复(可以是> 160)并将其拆分成多个文本的列表,每个文本<160。它也设置为保持文字整体。我把它包括在内:

repl='message to be sent. may be >160'
texts=[]
words=repl.split()
curtext=''
for word in words:
    #for the first word, drop the space
    if len(curtext)==0:
        curtext+=word

    #check if there's enough space left in the current message
    elif len(curtext)<=155-(len(word)+1):
        curtext+=' '+word

    #not enough space. make a new message
    else:
        texts.append(curtext)
        curtext=word
if curtext!='':
    texts.append(curtext)
return texts

但是,我现在想要对其进行修改,以便在每隔一条消息的末尾添加“reply m for more”。关于如何做到这一点的任何想法?

(我用Python编写代码)

2 个答案:

答案 0 :(得分:2)

reply = "text to be sent ...."
texts = []

count = 0
current_text = []
for word in reply.split():
   if count + len(word) < (160 if len(texts) % 2 == 0 else (160-17)):
      current_text.append(word)
      count += (len(word) + 1)
   else:
      count = 0
      if len(texts) % 2 != 0):
         #odd-numbered text gets additional message...
         texts.append(" ".join(current_text) + "\nreply m for more")
      else:
         texts.append(" ".join(current_text))
   current_text = []

答案 1 :(得分:0)

def sms_calculator(msg_text):
    sms_lst=[]
    if len(msg_text) == 0:
        return sms_lst

    l_m_text = (msg_text.split())
    if len(max(l_m_text, key=len))> 160:
        return sms_lst

    sms_string=l_m_text[0]
    for i in range(1,len(l_m_text)):
        if len(sms_string +' '+ l_m_text[i]) < 160 :
            sms_string=sms_string +' '+ l_m_text[i]
        else:
            sms_lst.append(sms_string)
            sms_string = l_m_text[i]
    sms_lst.append(sms_string)
    return sms_lst