about streamtoken

L

Lin Armis

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.

cheers,

Mian

_________________________________________________________________
Ãâ·ÑÏÂÔØ MSN Explorer: http://explorer.msn.com/lccn/
 
P

Peter Otten

Lin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top