Here's a puzzle...
Stephen Horne
steve at lurking.demon.co.uk
Sun Jul 22 04:35:26 EDT 2001
On 21 Jul 2001 23:56:40 GMT, thedustbustr at aol.com (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?
A string slice takes start and end positions - not start and end. The
positions are *between* character positions - or you could see the
range as including the start character but excluding the end
character.
For example...
>>> x="abcde"
>>> x[1:4]
'bcd'
Where the character positions are...
a b c d e
^ ^ ^ ^ ^ ^
| | | | | |
0 1 2 3 4 5
Taking that into account, your find function should search the whole
of data - not the slice of data that it is searching at present.
>And if you are up to it, feel free to help me splice (correct terminology,
>parse?) for variables rcpt and message ;)
Closer to lexical analysis (scanning - identifying keywords, numbers,
symbols etc) than parsing, though in context 'parsing' is a reasonable
word. Splice means gluing the bits back together again.
Anyway, try the following...
#################
import string
data=":Angel PRIVMSG Wiz :here is my message!"
# Separate header and body using split, discarding unwanted initial
# string of characters prior to first colon
(header,body) = string.split (data, ":") [1:]
# Or alternately, remove the initial colon before the split like
# this...
#
# (header,body) = string.split (data [1:], ":")
# Now separate the header fields - any whitespace is the default split
# seperator (even multiple spaces in a row count as a single
# separator)
(sender,command,rcpt) = string.split (header)
print "header : ", header
print "body : ", body
print
print "sender : ", sender
print "command : ", command
print "rcpt : ", rcpt
#################
The result I get is...
header : Angel PRIVMSG Wiz
body : here is my message!
sender : Angel
command : PRIVMSG
rcpt : Wiz
Hope that helps.
More information about the Python-list
mailing list