Determining Types in a String

npk1977 at my-deja.com npk1977 at my-deja.com
Tue Jun 20 10:23:44 EDT 2000


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