Here's a puzzle...

Paul Sidorsky paulsid at
Sat Jul 21 21:06:24 EDT 2001

TheDustbustr wrote:

> sender='Angel'
> print data[len(sender)+2:string.find(data[len(sender)+2:],' ')]
> And if you are up to it, feel free to help me splice (correct terminology,
> parse?) for variables rcpt and message ;)

By any chance are you coming from a C background?  :-)  That's a nasty
one-liner, so I'm not even going to attempt to figure out how it works. 
Instead, here is a much nicer way to do this kind of thing:

(This assumes the format is always the same, i.e. colons prefix each
section and single spaces separate the first three variables.)

def parsemsg(data):
    """Returns a tuple:  (sender, command, rcpt, message)"""
    tmp = data.split(':')
    hdr = tmp[1].split(' ')
    return (hdr[0], hdr[1], hdr[2], tmp[2])

If you have this in a module called parsemsg you can use it like this:

>>> teststring = ":Angel PRIVMSG Wiz :here is my message!"
>>> import procmsg
>>> sender, command, rcpt, message = parsemsg.parsemsg(teststring)
>>> print sender
>>> print command
>>> print rcpt
>>> print message
here is my message!

Hope that helps.

Paul Sidorsky                                          Calgary, Canada
paulsid at            

More information about the Python-list mailing list