[英]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.
我正在使用Mechanize和BeautifulSoup 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:
因此,我尝试执行以下操作:
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.