tokenise a string

M.E.Farmer mefjr75 at hotmail.com
Sat Oct 16 18:05:13 EDT 2004


Matthias Teege <matthias-dated at mteege.de> wrote in message 
> I have a input string like this:
> "name=matthias;count>10" or this "location!=thisone".

> 
> So I must parse the input, build tokens and map the fieldnames. Is
> there a special modul which I can use or are the standard string
> functions adequate?
> 
> Many thanks
> Matthias

This might be helpful.
Shlex can do much more, but I have not had the time to explore it.
#python shell#
>>> import shlex
>>> import cStringIO
>>> myFilelikeString = cStringIO.StringIO("name=matthias;count>10")
>>> lexer = shlex.shlex(myFilelikeString)
>>> while 1:
...     token = lexer.get_token()
...     if token:
...         print token
...     else:
...         break
...     
name
=
matthias
;
count
>

Also look at PySourceColor that I posted it covers token and tokenize.
HTH, 
  M.E.Farmer



More information about the Python-list mailing list