My first attempt at python
David Porter
jcm at bigskytel.com
Fri Dec 8 08:30:03 EST 2000
* Jason Stewart <kahuna01 at hotmail.com>:
> This same functionality might be in a module somewhere, but I
string.split()
> wrote it for practice. Its a string tokenizer. If you could analyze it
> and tell me where I could improve I would be grateful.
It looks good to me...
> def tokenize(string_to_chop):
> "Break a string up into individual words"
> last_space = 0
> token = []
> for index in range(len(string_to_chop)):
> if string_to_chop[index:index+1] == " ":
the above can be string_to_chop[index]
> token.append(string_to_chop[last_space:index])
> last_space = index + 1
> elif (index+1 == len(string_to_chop)):
> token.append(string_to_chop[last_space:index+1])
> return token
Here are two alternatives.
import re
def tokenize(s):
r = re.compile(' ')
return r.split(s)
def tokenize(s):
ls = list(s)
tokens = []
while ls:
try:
space = ls.index(' ')
except:
space = len(ls)
tokens.append(s[:space])
del ls[:space+1]
s = s[space+1:]
return tokens
Of course, the best way is:
import string
tokens = string.split(s)
(I wonder if this will reach the newsgroup...)
David
More information about the Python-list
mailing list