简体   繁体   English

在while循环中分割字符串并追加到列表

[英]Splitting string in while loop and appending to list

I'm working on a script to automate sending SMS messages through a website. 我正在研究一个脚本,以通过网站自动发送SMS消息。 I am using Mechanize and BeautifulSoup 4 for doing this. 我正在使用MechanizeBeautifulSoup 4来执行此操作。

The program works by calling it from the command line and passing the number and message as arguments; 该程序通过从命令行调用它并传递数字和消息作为参数来工作。 for this I am using Optparse . 为此,我正在使用Optparse

The message is passed to the program via the command line, but the website only accepts 444 characters per SMS message. 该消息是通过命令行传递给程序的,但该网站每条SMS消息仅接受444个字符。 So I am trying to do the following: 因此,我尝试执行以下操作:

  • determine the length of the message string (including whitespace) and IF greater than 444 then... 确定消息字符串的长度(包括空格),如果IF大于444,则...
  • iterate through a while loop which takes the temporary message string and appends the first 444 characters of the total message string from index 0 to a list object until the length of the temporary message string is no longer greater than 444 循环执行while循环,该循环获取临时消息字符串,并将总消息字符串的前444个字符从索引0追加到列表对象,直到临时消息字符串的长度不再大于444
  • and then by using the number of items in the list object I will iterate through a For Loop block which loops through the handling of sending the messages where each iteration corresponds to the index of a 444 character string (split of the total message) and I'll put that 444 character message slice in the appropriate HTML form field with Mechanize as the message to be sent (hopefully that is understandable!) 然后通过使用列表对象中的项目数,我将遍历一个For Loop块,该块循环遍历发送消息的处理,其中每次迭代都对应于444个字符串的索引(占总消息的数量),而I会将这444个字符的消息切片放在相应的HTML表单字段中,并以Mechanize作为要发送的消息(希望这是可以理解的!)

The code I have written so far is as follows: 到目前为止,我编写的代码如下:

message = "abcdefghijklmnopqrstuvwxyz..." # imagine it is > 444 characters
messageList = []
if len(message) > 444:
    tmpMsgString = message
    counter = 0
    msgLength = len(message)

    while msgLength > 444:
        messageList.append(tmpMsgString[counter:counter+445]) # 2nd index needs to point to last character's position in the string, not "counter+445" because this would cause an error when there isn't enough characters in string?
        tmpMsgString = tmpMsgString[counter+445:msgLength])
        msgLength = msgLength-444
        counter = counter + 444
else:
    messageList.append(message)

I can manage the portion of the code to accept the arguments from the command line and I can also manage with looping through a for loop block and using each item within the list as the message to be sent, however I have little Python experience and I need an experienced pair of eyes to help me along with this part of the code! 我可以管理代码的一部分以从命令行接受参数,也可以通过循环for循环块并将列表中的每个项目用作要发送的消息来进行管理,但是我很少有Python经验,我需要一双经验丰富的眼睛来帮助我与代码的这一部分一起! All help appreciated. 所有帮助表示赞赏。

Batteries included. 包括电池。 This uses 44 chars, for demonstration purposes. 出于演示目的,它使用了44个字符。 The resulting list can easily be iterated over. 结果列表可以轻松地进行迭代。 Plus it splits at word boundaries, not arbitrarily. 另外,它在单词边界处分割,而不是任意分割。

>>> import textwrap
>>> s = "lorem ipsum" * 20
>>> textwrap.wrap(s, width=44)
['lorem ipsumlorem ipsumlorem ipsumlorem', 'ipsumlorem ipsumlorem ipsumlorem ipsumlorem', 'ipsumlorem ipsumlorem ipsumlorem ipsumlor
em', 'ipsumlorem ipsumlorem ipsumlorem ipsumlorem', 'ipsumlorem ipsumlorem ipsumlorem ipsumlorem', 'ipsum']

If all you need to do is split up the string into 444-character chunks, there isn't any need for a counter or complicated stuff. 如果您需要做的只是将字符串分成444个字符的块,则不需要计数器或复杂的东西。 Here's how you can update your current code: 您可以通过以下方式更新当前代码:

message = "whatever..."*1000
tmp = message
msgList = []
while tmp:
    msgList.append(tmp[:444])
    tmp = tmp[444:]

This will work because slices that span outside the range of a sequence will be truncated to the end of the sequence (no IndexError s will be raised). 这将起作用,因为跨越序列范围的片段将被截断到序列的末尾(不会引发IndexError )。 If the whole slice is out of bounds, the result will be empty. 如果整个切片超出范围,则结果为空。

You might be able to do this a bit better using a list comprehension: 使用列表理解,您也许可以做得更好:

message = "whatever"*1000
msgList = [message[i:i+444] for i in range(0, len(message), 444)]

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

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