-1

I'm working on a script to automate sending SMS messages through a website. I am using Mechanize and BeautifulSoup 4 for doing this.

The program works by calling it from the command line and passing the number and message as arguments; for this I am using Optparse.

The message is passed to the program via the command line, but the website only accepts 444 characters per SMS message. So I am trying to do the following:

  • determine the length of the message string (including whitespace) and IF greater than 444 then...
  • 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
  • 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!)

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! All help appreciated.

askewchan
  • 45,161
  • 17
  • 118
  • 134
uncle-junky
  • 723
  • 1
  • 8
  • 33
  • 1
    1: Line lengths. ... 2: What's not working? You haven't told us what the problem is. – Veedrac Sep 25 '13 at 19:56
  • Related: [How do you split a list into evenly sized chunks in Python?](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) – Ashwini Chaudhary Sep 25 '13 at 19:58
  • Apologies. I haven't tested it, however it is likely to be wrong somewhere, in particular the way I am trying to remove characters and update the variable tmpMsgString on each iteration of the while loop. – uncle-junky Sep 25 '13 at 20:00

2 Answers2

4

Batteries included. This uses 44 chars, for demonstration purposes. 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']
jhermann
  • 2,071
  • 13
  • 17
2

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. 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 IndexErrors will be raised). 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)]
Blckknght
  • 100,903
  • 11
  • 120
  • 169