you - python string to decimal
How do I parse a string to a float or int in Python? (16)
In Python, how can I parse a numeric string like
"545.2222" to its corresponding float value,
542.2222? Or parse the string
"31" to an integer,
I just want to know how to parse a float
string to a
float, and (separately) an int
string to an
In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222? Or parse the string "31" to an integer, 31? I just want to know how to parse a float string to a float, and (separately) an int string to an int.
It's good that you ask to do these separately. If you're mixing them, you may be setting yourself up for problems later. The simple answer is:
"545.2222" to float:
>>> float("545.2222") 545.2222
"31" to an integer:
>>> int("31") 31
Other conversions, ints to and from strings and literals:
Conversions from various bases, and you should know the base in advance (10 is the default). Note you can prefix them with what Python expects for its literals (see below) or remove the prefix:
>>> int("0b11111", 2) 31 >>> int("11111", 2) 31 >>> int('0o37', 8) 31 >>> int('37', 8) 31 >>> int('0x1f', 16) 31 >>> int('1f', 16) 31
If you don't know the base in advance, but you do know they will have the correct prefix, Python can infer this for you if you pass
0 as the base:
>>> int("0b11111", 0) 31 >>> int('0o37', 0) 31 >>> int('0x1f', 0) 31
Non-Decimal (i.e. Integer) Literals from other Bases
If your motivation is to have your own code clearly represent hard-coded specific values, however, you may not need to convert from the bases - you can let Python do it for you automatically with the correct syntax.
You can use the apropos prefixes to get automatic conversion to integers with the following literals. These are valid for Python 2 and 3:
>>> 0b11111 31
>>> 0o37 31
>>> 0x1f 31
This can be useful when describing binary flags, file permissions in code, or hex values for colors - for example, note no quotes:
>>> 0b10101 # binary flags 21 >>> 0o755 # read, write, execute perms for owner, read & ex for group & others 493 >>> 0xffffff # the color, white, max values for red, green, and blue 16777215
Making ambiguous Python 2 octals compatible with Python 3
If you see an integer that starts with a 0, in Python 2, this is (deprecated) octal syntax.
>>> 037 31
It is bad because it looks like the value should be
37. So in Python 3, it now raises a
>>> 037 File "<stdin>", line 1 037 ^ SyntaxError: invalid token
Convert your Python 2 octals to octals that work in both 2 and 3 with the
>>> 0o37 31
Localization and commas
You should consider the possibility of commas in the string representation of a number, for cases like
float("545,545.2222") which throws an exception. Instead, use methods in
locale to convert the strings to numbers and interpret commas correctly. The
locale.atof method converts to a float in one step once the locale has been set for the desired number convention.
Example 1 -- United States number conventions
In the United States and the UK, commas can be used as a thousands separator. In this example with American locale, the comma is handled properly as a separator:
>>> import locale >>> a = u'545,545.2222' >>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') 'en_US.UTF-8' >>> locale.atof(a) 545545.2222 >>> int(locale.atof(a)) 545545 >>>
Example 2 -- European number conventions
In the majority of countries of the world, commas are used for decimal marks instead of periods. In this example with French locale, the comma is correctly handled as a decimal mark:
>>> import locale >>> b = u'545,2222' >>> locale.setlocale(locale.LC_ALL, 'fr_FR') 'fr_FR' >>> locale.atof(b) 545.2222
locale.atoi is also available, but the argument should be an integer.
This is a corrected version of https://.com/a/33017514/5973334
This will try to parse a string and return either
float depending on what the string represents.
It might rise parsing exceptions or have some unexpected behaviour.
def get_int_or_float(v): number_as_float = float(v) number_as_int = int(number_as_float) return number_as_int if number_as_float == number_as_int else number_as_float
I am surprised nobody mentioned regex because sometimes string must be prepared and normalized before casting to number
import re def parseNumber(value, as_int=False): try: number = float(re.sub('[^.\-\d]', '', value)) if as_int: return int(number + 0.5) else: return number except ValueError: return float('nan') # or None if you wish
parseNumber('13,345') > 13345.0 parseNumber('- 123 000') > -123000.0 parseNumber('99999\n') > 99999.0
and by the way, something to verify you have a number:
import numbers def is_number(value): return isinstance(value, numbers.Number) # will work with int, float, long, Decimal
I use this function for that
import ast def parse_str(s): try: return ast.literal_eval(str(s)) except: return
It will convert the string to its type
value = parse_str('1') # Returns Integer value = parse_str('1.5') # Returns Float
Python have this great flexibility of parsing in one liners.
str = "545.2222" print ("int: ", + int(float(a))) print ("float: ", +(float(a)))
The YAML parser can help you figure out what datatype your string is. Use
yaml.load(), and then you can use
type(result) to test for type:
>>> import yaml >>> a = "545.2222" >>> result = yaml.load(a) >>> result 545.22220000000004 >>> type(result) <type 'float'> >>> b = "31" >>> result = yaml.load(b) >>> result 31 >>> type(result) <type 'int'> >>> c = "HI" >>> result = yaml.load(c) >>> result 'HI' >>> type(result) <type 'str'>
This is another method which deserves to be mentioned here, ast.literal_eval:
This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.
That is, a safe 'eval'
>>> import ast >>> ast.literal_eval("545.2222") 545.2222 >>> ast.literal_eval("31") 31
To typecast in python use the constructor funtions of the type, passing the string (or whatever value you are trying to cast) as a parameter.
Behind the scenes, python is calling the objects
__float__ method, which should return a float representation of the parameter. This is especially powerful, as you can define your own types (using classes) with a
__float__ method so that it can be casted into a float using float(myobject).
def num(s): try: for each in s: yield int(each) except ValueError: yield float(each) a = num(["123.55","345","44"]) print a.next() print a.next()
This is the most Pythonic way I could come up with.
Users codelogic and harley are correct, but keep in mind if you know the string is an integer (for example, 545) you can call int("545") without first casting to float.
If your strings are in a list, you could use the map function as well.
>>> x = ["545.0", "545.6", "999.2"] >>> map(float, x) [545.0, 545.60000000000002, 999.20000000000005] >>>
It is only good if they're all the same type.
You need to take into account rounding to do this properly.
I.e. int(5.1) => 5 int(5.6) => 5 -- wrong, should be 6 so we do int(5.6 + 0.5) => 6
def convert(n): try: return int(n) except ValueError: return float(n + 0.5)
>>> a = "545.2222" >>> float(a) 545.22220000000004 >>> int(float(a)) 545
def num(s): try: return int(s) except ValueError: return float(s)
def num(s): """num(s) num(3),num(3.7)-->3 num('3')-->3, num('3.7')-->3.7 num('3,700')-->ValueError num('3a'),num('a3'),-->ValueError num('3e4') --> 30000.0 """ try: return int(s) except ValueError: try: return float(s) except ValueError: raise ValueError('argument is not a string of number')