[Tutor] Re: integer

Derrick 'dman' Hudson dman@dman.ddts.net
Sun, 9 Jun 2002 20:13:11 -0500


--IS0zKkzwUGydFO0o
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sun, Jun 09, 2002 at 08:17:29PM -0400, Ricardo Ortega wrote:
| How can I get an integer from a string example: ?BLK 15423 L?
| How can I get just the integer from that string if I didn?t know what it
| looked like before hand all I new is that there was a number somewhere
| in the string.

In general, parsing an arbitrary string with no predefined structure
is impossible.  If I throw random data at you, how would you make any
sense out of it, unless we first agree on some structure?  Random data
looks much like grafiti does.

OTOH, if you know something about the string, for example the one you
gave above, it can be parsed :

# start with the data
s_org =3D "?BLK 15423 L?"

# split it on the spaces, and keep just the number part
s_num =3D s_org.split( ' ' )[1]

# now try and convert it to an integer.  be aware that if the data is
# malformed (eg a corrupt file) the conversion will fail
try :
    number =3D int( s_num )
except ValueError , err :
    print "Couldn't convert '%s' to a number.\n%s" % ( s_num , str( err ) )

HTH,
-D

--=20

Microsoft is to operating systems & security ....
                                     .... what McDonald's is to gourmet coo=
king
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--IS0zKkzwUGydFO0o
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj0D/ScACgkQO8l8XBKTpRTrYgCfVMXM+Gz6+6knTJiOp2FwpaNA
uWEAn3sYeRtDFPYbis4M0DtehjcqLgtt
=hFWH
-----END PGP SIGNATURE-----

--IS0zKkzwUGydFO0o--