In Python, how can I parse a numeric string like \"545.2222\"
to its corresponding float value, 545.2222
? Or parse the string \"31\"
t
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)
>>> b = "31"
>>> result = yaml.load(b)
>>> result
31
>>> type(result)
>>> c = "HI"
>>> result = yaml.load(c)
>>> result
'HI'
>>> type(result)