Tri*_*ick 2108 python floating-point parsing integer type-conversion
在Python中,我如何解析数字字符串,如"545.2222"
相应的浮点值,545.2222
?或者将字符串解析为"31"
整数,31
?
我只是想知道如何将一个浮点数 解析str
为a float
,并且(单独)将一个int 解析str
为一个int
.
Har*_*mbe 2473
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Run Code Online (Sandbox Code Playgroud)
Jav*_*ier 492
def num(s):
try:
return int(s)
except ValueError:
return float(s)
Run Code Online (Sandbox Code Playgroud)
Eri*_*ski 481
def is_float(value):
try:
float(value)
return True
except:
return False
Run Code Online (Sandbox Code Playgroud)
此功能的更长,更准确的名称可能是: is_convertible_to_float(value)
val is_float(val) Note
-------------------- ---------- --------------------------------
"" False Blank string
"127" True Passed string
True True Pure sweet Truth
"True" False Vile contemptible lie
False True So false it becomes true
"123.456" True Decimal
" -127 " True Spaces trimmed
"\t\n12\r\n" True whitespace ignored
"NaN" True Not a number
"NaNanananaBATMAN" False I am Batman
"-iNF" True Negative infinity
"123.E4" True Exponential notation
".1" True mantissa only
"1,234" False Commas gtfo
u'\x30' True Unicode is fine.
"NULL" False Null is not special
0x3fade True Hexadecimal
"6e7777777777777" True Shrunk to infinity
"1.797693e+308" True This is max value
"infinity" True Same as inf
"infinityandBEYOND" False Extra characters wreck it
"12.34.56" False Only one dot allowed
u'?' False Japanese '4' is not a float.
"#56" False Pound sign
"56%" False Percent of what?
"0E0" True Exponential, move dot 0 places
0**0 True 0___0 Exponentiation
"-5e-5" True Raise to a negative number
"+1e1" True Plus is OK with exponent
"+1e1^5" False Fancy exponent not interpreted
"+1e1.3" False No decimals in exponent
"-+1" False Make up your mind
"(1)" False Parenthesis is bad
Run Code Online (Sandbox Code Playgroud)
你认为你知道什么数字?你没有想象的那么好!不是很大的惊喜.
wim*_*wim 122
这是另一种值得在此提及的方法,ast.literal_eval:
这可以用于安全地评估包含来自不可信来源的Python表达式的字符串,而无需自己解析值.
也就是说,安全'评估'
>>> import ast
>>> ast.literal_eval("545.2222")
545.2222
>>> ast.literal_eval("31")
31
Run Code Online (Sandbox Code Playgroud)
小智 76
float(x) if '.' in x else int(x)
Run Code Online (Sandbox Code Playgroud)
Mar*_*ian 63
您应该考虑数字的字符串表示形式中逗号的可能性,例如 float("545,545.2222")
抛出异常的情况.相反,使用方法locale
将字符串转换为数字并正确解释逗号.locale.atof
一旦为所需的数字约定设置了语言环境,该方法就会在一个步骤中转换为浮点数.
示例1 - 美国号码惯例
在美国和英国,逗号可以用作千位分隔符.在使用美国语言环境的此示例中,逗号作为分隔符正确处理:
>>> 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
>>>
Run Code Online (Sandbox Code Playgroud)
示例2 - 欧洲数字惯例
在世界上大多数国家/地区,逗号用于十进制标记而不是句点.在此示例中,使用法语区域设置,逗号可以正确处理为小数点:
>>> import locale
>>> b = u'545,2222'
>>> locale.setlocale(locale.LC_ALL, 'fr_FR')
'fr_FR'
>>> locale.atof(b)
545.2222
Run Code Online (Sandbox Code Playgroud)
该方法locale.atoi
也可用,但参数应为整数.
Set*_*ton 25
如果您不反对第三方模块,可以查看fastnumbers模块.它提供了一个名为fast_real的函数,它完全符合这个问题的要求,并且比纯Python实现更快:
>>> from fastnumbers import fast_real
>>> fast_real("545.2222")
545.2222
>>> type(fast_real("545.2222"))
float
>>> fast_real("31")
31
>>> type(fast_real("31"))
int
Run Code Online (Sandbox Code Playgroud)
小智 24
用户codelogic和harley是正确的,但请记住,如果你知道字符串是一个整数(例如,545),你可以调用int("545")而不首先转换为float.
如果您的字符串在列表中,您也可以使用map函数.
>>> x = ["545.0", "545.6", "999.2"]
>>> map(float, x)
[545.0, 545.60000000000002, 999.20000000000005]
>>>
Run Code Online (Sandbox Code Playgroud)
如果他们都是同一类型,那就好了.
krz*_*zym 19
这个问题似乎有点老了.但是,让我建议一个函数,parseStr,它使类似的东西,即返回整数或浮点数,如果给定的ASCII字符串不能转换为它们中的任何一个,它返回它不受影响.代码当然可以调整为只做你想要的:
>>> import string
>>> parseStr = lambda x: x.isalpha() and x or x.isdigit() and \
... int(x) or x.isalnum() and x or \
... len(set(string.punctuation).intersection(x)) == 1 and \
... x.count('.') == 1 and float(x) or x
>>> parseStr('123')
123
>>> parseStr('123.3')
123.3
>>> parseStr('3HC1')
'3HC1'
>>> parseStr('12.e5')
1200000.0
>>> parseStr('12$5')
'12$5'
>>> parseStr('12.2.2')
'12.2.2'
Run Code Online (Sandbox Code Playgroud)
Aar*_*all 19
在Python中,如何将像"545.2222"这样的数字字符串解析为相应的浮点值542.2222?或者将字符串"31"解析为整数,31? 我只想知道如何将浮点字符串解析为float,并(单独)将int字符串解析为int.
你要求分别做这些是很好的.如果你正在混合它们,你可能会在以后为自己设置问题.简单的答案是:
"545.2222"
漂浮:
>>> float("545.2222")
545.2222
Run Code Online (Sandbox Code Playgroud)
"31"
到整数:
>>> int("31")
31
Run Code Online (Sandbox Code Playgroud)
各种基础的转换,你应该事先知道基数(10是默认值).请注意,您可以使用Python期望的文字前缀(参见下文)或删除前缀:
>>> int("0b11111", 2)
31
>>> int("11111", 2)
31
>>> int('0o37', 8)
31
>>> int('37', 8)
31
>>> int('0x1f', 16)
31
>>> int('1f', 16)
31
Run Code Online (Sandbox Code Playgroud)
如果您事先不知道基数,但是您知道它们将具有正确的前缀,那么如果您0
作为基数传递,Python可以为您推断:
>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31
Run Code Online (Sandbox Code Playgroud)
如果您的动机是让您自己的代码清楚地表示硬编码的特定值,那么您可能不需要从基础转换 - 您可以让Python使用正确的语法自动为您完成.
您可以使用apropos前缀自动转换为具有以下文字的整数.这些对Python 2和3有效:
二进制,前缀 0b
>>> 0b11111
31
Run Code Online (Sandbox Code Playgroud)
八进制,前缀 0o
>>> 0o37
31
Run Code Online (Sandbox Code Playgroud)
十六进制,前缀 0x
>>> 0x1f
31
Run Code Online (Sandbox Code Playgroud)
这在描述二进制标志,代码中的文件权限或颜色的十六进制值时非常有用 - 例如,请注意没有引号:
>>> 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
Run Code Online (Sandbox Code Playgroud)
如果你看到一个以0开头的整数,在Python 2中,这是(不推荐的)八进制语法.
>>> 037
31
Run Code Online (Sandbox Code Playgroud)
这很糟糕,因为它看起来应该是值37
.所以在Python 3中,它现在提出了一个SyntaxError
:
>>> 037
File "<stdin>", line 1
037
^
SyntaxError: invalid token
Run Code Online (Sandbox Code Playgroud)
将您的Python 2八进制转换为在2和3中均使用0o
前缀的八进制:
>>> 0o37
31
Run Code Online (Sandbox Code Playgroud)
Raf*_*afe 13
该YAML解析器可以帮助你找出什么样的数据类型的字符串.使用yaml.load()
,然后你可以type(result)
用来测试类型:
>>> 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'>
Run Code Online (Sandbox Code Playgroud)
Sha*_*eem 13
我使用这个功能
import ast
def parse_str(s):
try:
return ast.literal_eval(str(s))
except:
return
Run Code Online (Sandbox Code Playgroud)
它会将字符串转换为其类型
value = parse_str('1') # Returns Integer
value = parse_str('1.5') # Returns Float
Run Code Online (Sandbox Code Playgroud)
Tot*_*oro 11
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
Run Code Online (Sandbox Code Playgroud)
U10*_*ard 10
你可以使用json.loads
:
>>> import json
>>> json.loads('123.456')
123.456
>>> type(_)
<class 'float'>
>>>
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它变成了一种float
.
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')
Run Code Online (Sandbox Code Playgroud)
您需要考虑舍入才能正确执行此操作.
即int(5.1)=> 5 int(5.6)=> 5 - 错误,应该是6所以我们做int(5.6 + 0.5)=> 6
def convert(n):
try:
return int(n)
except ValueError:
return float(n + 0.5)
Run Code Online (Sandbox Code Playgroud)
将您的字符串传递给此函数:
def string_to_number(str):
if("." in str):
try:
res = float(str)
except:
res = str
elif(str.isdigit()):
res = int(str)
else:
res = str
return(res)
Run Code Online (Sandbox Code Playgroud)
它将根据传递的内容返回 int、float 或 string。
一个 int 类型的字符串
print(type(string_to_number("124")))
<class 'int'>
Run Code Online (Sandbox Code Playgroud)
作为浮点数的字符串
print(type(string_to_number("12.4")))
<class 'float'>
Run Code Online (Sandbox Code Playgroud)
作为字符串的字符串
print(type(string_to_number("hello")))
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
看起来像浮点数的字符串
print(type(string_to_number("hel.lo")))
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
我很惊讶没有人提到正则表达式,因为有时字符串必须准备并在转换为数字之前进行规范化
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
Run Code Online (Sandbox Code Playgroud)
用法:
parseNumber('13,345')
> 13345.0
parseNumber('- 123 000')
> -123000.0
parseNumber('99999\n')
> 99999.0
Run Code Online (Sandbox Code Playgroud)
顺便说一句,要验证你有一个数字:
import numbers
def is_number(value):
return isinstance(value, numbers.Number)
# will work with int, float, long, Decimal
Run Code Online (Sandbox Code Playgroud)
小智 5
要在python中进行类型转换,请使用该类型的构造函数,并将字符串(或您尝试投射的任何值)作为参数传递。
例如:
>>>float("23.333")
23.333
Run Code Online (Sandbox Code Playgroud)
在后台,python正在调用objects __float__
方法,该方法应该返回参数的float表示形式。这是特别强大的功能,因为您可以使用__float__
方法定义自己的类型(使用类),以便可以使用float(myobject)将其转换为float。
处理十六进制、八进制、二进制、十进制和浮点数
此解决方案将处理数字的所有字符串约定(我所知道的所有内容)。
def to_number(n):
''' Convert any number representation to a number
This covers: float, decimal, hex, and octal numbers.
'''
try:
return int(str(n), 0)
except:
try:
# python 3 doesn't accept "010" as a valid octal. You must use the
# '0o' prefix
return int('0o' + n, 0)
except:
return float(n)
Run Code Online (Sandbox Code Playgroud)
这个测试用例输出说明了我在说什么。
======================== CAPTURED OUTPUT =========================
to_number(3735928559) = 3735928559 == 3735928559
to_number("0xFEEDFACE") = 4277009102 == 4277009102
to_number("0x0") = 0 == 0
to_number(100) = 100 == 100
to_number("42") = 42 == 42
to_number(8) = 8 == 8
to_number("0o20") = 16 == 16
to_number("020") = 16 == 16
to_number(3.14) = 3.14 == 3.14
to_number("2.72") = 2.72 == 2.72
to_number("1e3") = 1000.0 == 1000
to_number(0.001) = 0.001 == 0.001
to_number("0xA") = 10 == 10
to_number("012") = 10 == 10
to_number("0o12") = 10 == 10
to_number("0b01010") = 10 == 10
to_number("10") = 10 == 10
to_number("10.0") = 10.0 == 10
to_number("1e1") = 10.0 == 10
Run Code Online (Sandbox Code Playgroud)
这是测试:
class test_to_number(unittest.TestCase):
def test_hex(self):
# All of the following should be converted to an integer
#
values = [
# HEX
# ----------------------
# Input | Expected
# ----------------------
(0xDEADBEEF , 3735928559), # Hex
("0xFEEDFACE", 4277009102), # Hex
("0x0" , 0), # Hex
# Decimals
# ----------------------
# Input | Expected
# ----------------------
(100 , 100), # Decimal
("42" , 42), # Decimal
]
values += [
# Octals
# ----------------------
# Input | Expected
# ----------------------
(0o10 , 8), # Octal
("0o20" , 16), # Octal
("020" , 16), # Octal
]
values += [
# Floats
# ----------------------
# Input | Expected
# ----------------------
(3.14 , 3.14), # Float
("2.72" , 2.72), # Float
("1e3" , 1000), # Float
(1e-3 , 0.001), # Float
]
values += [
# All ints
# ----------------------
# Input | Expected
# ----------------------
("0xA" , 10),
("012" , 10),
("0o12" , 10),
("0b01010" , 10),
("10" , 10),
("10.0" , 10),
("1e1" , 10),
]
for _input, expected in values:
value = to_number(_input)
if isinstance(_input, str):
cmd = 'to_number("{}")'.format(_input)
else:
cmd = 'to_number({})'.format(_input)
print("{:23} = {:10} == {:10}".format(cmd, value, expected))
self.assertEqual(value, expected)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3714955 次 |
最近记录: |