about streamtoken

Peter Otten __peter__ at web.de
Sun Apr 11 03:05:48 EDT 2004


Lin Armis wrote:

> hi all:
> In java,there is streamtoken method to deal with input string.
> how does the python do this work?
> Supposed that input "aa/dd/as/23/a22",I wanna  the string "23" change to
> int('23'),but how can I tell the diffrent between '23'and 'aa' in the all
> strings?in java ,I can use streamtoken to recognize the number type and
> string type from input,but i dont know how is in Python.

Here is one simplistic way to do it:

>>> s = "aa/dd/as/23/a22"
>>> def convert(s, converters=[int, float]):
...     for c in converters:
...             try:
...                     return c(s)
...             except ValueError:
...                     pass
...     return s
...
>>> a = [convert(t) for t in s.split("/")]
>>> a
['aa', 'dd', 'as', 23, 'a22']

Use introspection to detect a token's type:
>>> isinstance(a[0], int)
False
>>> isinstance(a[3], int)
True

For more complicated input you could use regular expressions.

Peter




More information about the Python-list mailing list