Determining Types in a String
William Dandreta
wjdandreta at worldnet.att.net
Wed Jun 21 20:35:06 EDT 2000
Hi n,
Have you tried putting the types like 'string', 'int', etc, as keys in a
dictionary something like this:
myTypes = {'int': doint, 'string': dostring, etc.}
fields = string.split(line)
try:
for f in fields:
myTypes[type(f)]
except:
do something if type not listed
npk1977 at my-deja.com wrote in message <8inuov$8r2$1 at nnrp1.deja.com>...
>Hi all, I am trying to load a matrix of floats, ints and strings into
>an array.
>
>I have a method that works, but is slow, the psuedocode is:
>
>v = string.split(line)
>for items in v:
> Attempt to recast item as an int(), if that fails, catch the exception
>and try to recase as long(), keep doing this until I can go no further.
>
> 6 def Recast(v):
> 7 v = string.split(v)
> 8 for i in range(0,len(v)):
> 9 try: v[i] = int(v[i])
>10 except Exception:
>11 try: v[i] = long(v[i])
>12 except Exception:
>13 try: v[i] = float(v[i])
>14 except Exception:
>15 try: v[i] = str(v[i])
>16 except Exception:
>17 FAIL
>
>Thanks!
>n
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.
More information about the Python-list
mailing list