Here's a puzzle...

Tom Jenkins tjenkins at nospiced.ham.devis.com
Sat Jul 21 21:16:48 EDT 2001


TheDustbustr wrote:

> I'm trying to split the following string called data into four variables:
> 
> :Angel PRIVMSG Wiz :here is my message!
> 
> (so data=':Angel PRIVMSG Wiz :here is my message!') with an outcome of:
> 
> sender='Angel'
> command='PRIVMSG'
> rcpt=Wiz'
> message='here is my message!'
> 
> Unless my logic is flawed, this should return 'PRIVMSG':
> 
> sender='Angel'
> print data[len(sender)+2:string.find(data[len(sender)+2:],' ')]
> 
> It prints '' (an empty string).  Is my logic flawed, or does Python dislike a
> string splice determined by a function?
> 
> And if you are up to it, feel free to help me splice (correct terminology,
> parse?) for variables rcpt and message ;)
> 
> Many thanks, Dustin
> 

Well if your string data is always formated that way, then this function 
will parse it into the parts:

 >>> def splitMessage(data):
... 	dummy, set, msg = data.split(':')
... 	sender, command, rcpt = set.split()
... 	return (sender, command, rcpt, msg)
...
 >>> s, c, r, m = splitMessage(data)
 >>> print s
Angel
 >>> print c
PRIVMSG
 >>> print r
Wiz
 >>> print m
here is my message!
 >>> print data
:Angel PRIVMSG Wiz :here is my message!
 >>>

however if the sender or rcpt fields are allowed to contain spaces, then 
it won't work.

but-hey-i-didn't-get-any-specs-ly yours
Tom




More information about the Python-list mailing list