Parsing strings -> numbers

Miki Tebeka mikit at zoran.co.il
Tue Nov 25 01:32:48 EST 2003


Hello Tuang,

> In C#, you can say
> 
> int.Parse(myString)
>
> and it will turn a string like "-12,345" into a proper int. It works
> for all sorts of data types with all sorts of formats, and you can
> pass it locale parameters to tell it, for example, to parse a German
> "12.345,67" into 12345.67. Java does this, too.
> (Integer.parseInt(myStr), IIRC).
> 
> What's the equivalent in Python?
Python has a build in "int", "long" and "float" functions. However
they are more limited than what you want.
 
> And if the only problem is comma thousand-separators (e.g.,
> "12,345.67"), is there a higher-performance way to convert that into
> the number 12345.67 than using Python's formal parsers?
i = int("12,345.67".replace(",", ""))

HTH.
Miki




More information about the Python-list mailing list