如何将任何数字字符串值转换为 int?
How to convert any numeric string value in int?
我想将 0x123
、012
或 36#foo
转换为整数。
为此,我这样写:
def str2int(s):
if re.findall('(?i)^0x',s):
return int(s, 16)
if re.findall('^(?i)0',s):
return int(s, 8)
m = re.findall('(?i)^(\d+)\#([0-9a-z]+)',s)
if m:
return int(m[0][1], m[0][0])
raise AssertionError, 'Unknown value'
感觉有点复杂。有内置方法吗?
是的,您可以使用 ast.literal_eval
。
>>> import ast
>>> ast.literal_eval("0x123")
291
>>> ast.literal_eval("012")
10
>>> ast.literal_eval("36#foo")
36
但是,请注意 literal_eval("012")
只能在 2.7 及更低版本中使用,因为 3.x 不再支持这种八进制文字样式。但这会起作用:
>>> ast.literal_eval("0o12")
10
int
会做,当你传递 0
作为第二个参数时:
int('0x123', 0)
=> 291
int('0o12', 0)
=> 10
如果要支持评论,str.partition
是我能想到的最简单的方法:
int('36#foo'.partition('#')[0], 0)
=> 36
没有正则表达式的解决方案:
def convert (s):
if s.lower().startswith('0x'):
s = '16#' + s[2:]
elif s.startswith('0'):
s = '8#' + s[1:]
elif '#' not in s:
s = '10#' + s
base, num = s.split('#', 1)
return int(num, int(base))
>>> testcases = [('0x123', 291), ('012', 10), ('36#foo', 20328)]
>>> for s, n in testcases:
print(s, n, n == convert(s))
0x123 291 True
012 10 True
36#foo 20328 True
我想将 0x123
、012
或 36#foo
转换为整数。
为此,我这样写:
def str2int(s):
if re.findall('(?i)^0x',s):
return int(s, 16)
if re.findall('^(?i)0',s):
return int(s, 8)
m = re.findall('(?i)^(\d+)\#([0-9a-z]+)',s)
if m:
return int(m[0][1], m[0][0])
raise AssertionError, 'Unknown value'
感觉有点复杂。有内置方法吗?
是的,您可以使用 ast.literal_eval
。
>>> import ast
>>> ast.literal_eval("0x123")
291
>>> ast.literal_eval("012")
10
>>> ast.literal_eval("36#foo")
36
但是,请注意 literal_eval("012")
只能在 2.7 及更低版本中使用,因为 3.x 不再支持这种八进制文字样式。但这会起作用:
>>> ast.literal_eval("0o12")
10
int
会做,当你传递 0
作为第二个参数时:
int('0x123', 0)
=> 291
int('0o12', 0)
=> 10
如果要支持评论,str.partition
是我能想到的最简单的方法:
int('36#foo'.partition('#')[0], 0)
=> 36
没有正则表达式的解决方案:
def convert (s):
if s.lower().startswith('0x'):
s = '16#' + s[2:]
elif s.startswith('0'):
s = '8#' + s[1:]
elif '#' not in s:
s = '10#' + s
base, num = s.split('#', 1)
return int(num, int(base))
>>> testcases = [('0x123', 291), ('012', 10), ('36#foo', 20328)]
>>> for s, n in testcases:
print(s, n, n == convert(s))
0x123 291 True
012 10 True
36#foo 20328 True