如何从字符串中提取数字才能操作它?该号码可以是int
或float
。例如,如果字符串为"flour, 100, grams"
或"flour, 100.5, grams"
,则提取数字100
或100.5
。
代码:
string = "flour, 100, grams"
numbers = [int(x) for x in string.split(",")]
print(numbers)
输出:
Traceback (most recent call last):
File "/Users/lewis/Documents/extracting numbers.py", line 2, in <module>
numbers = [int(x) for x in string.split(",")]
File "/Users/lewis/Documents/extracting numbers.py", line 2, in <listcomp>
numbers = [int(x) for x in string.split(",")]
ValueError: invalid literal for int() with base 10: 'flour'
答案 0 :(得分:4)
考虑到字符串的结构,当您使用str.split
将字符串拆分为三个字符串的列表时,您应该只使用以下三个元素之一:
>>> s = "flour, 100, grams"
>>> s.split(",")
['flour', ' 100', ' grams']
>>> s.split(",")[1] # index the middle element (Python is zero-based)
' 100'
然后,您可以使用float
将该字符串转换为数字:
>>> float(s.split(",")[1])
100.0
如果您不能确定字符串的结构,可以使用re
(正则表达式)来提取数字,使用map
将它们全部转换为:
>>> import re
>>> map(float, re.findall(r"""\d+ # one or more digits
(?: # followed by...
\. # a decimal point
\d+ # and another set of one or more digits
)? # zero or one times""",
"Numbers like 1.1, 2, 34 and 15.16.",
re.VERBOSE))
[1.1, 2.0, 34.0, 15.16]
答案 1 :(得分:1)
你有没有试过尝试除了你的类型演员周围的块,这将扔掉字符串面粉,但保持100
string = 'flour, 100, grams'
numbers = []
for i in string.split(','):
try:
print int(i)
numbers.append(i)
except: pass
答案 2 :(得分:0)
给自己写一个小转换函数,如下面的那个试图将其参数首先转换为int
,然后转换为float
,然后转换为complex
(仅扩展示例) )。如果您希望获得/保留最合适的输入类型,那么尝试转换的顺序非常重要,因为int
将成功转换为float
,但反之亦然,因此您需要尝试首先将输入转换为int
。
def convert_to_number(n):
candidate_types = (int, float, complex)
for t in candidate_types:
try:
return t(str(n))
except ValueError:
# pass
print "{!r} is not {}".format(n, t) # comment out if not debugging
else:
raise ValueError('{!r} can not be converted to any of: {}'.format(n, candidate_types))
>>> s = "flour, 100, grams"
>>> n = convert_to_number(s.split(',')[1])
>>> type(n)
<type 'int'>
>>> n
100
>>> s = "flour, 100.123, grams"
>>> n = convert_to_number(s.split(',')[1])
' 100.123' is not <type 'int'>
>>> type(n)
<type 'float'>
>>> n
100.123
>>> n = convert_to_number('100+20j')
'100+20j' is not <type 'int'>
'100+20j' is not <type 'float'>
>>> type(n)
<type 'complex'>
>>> n
(100+20j)
>>> n = convert_to_number('one')
'one' is not <type 'int'>
'one' is not <type 'float'>
'one' is not <type 'complex'>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/ctn.py", line 10, in convert_to_number
raise ValueError('{!r} can not be converted to any of: {}'.format(n, candidate_types))
ValueError: 'one' can not be converted to any of: (<type 'int'>, <type 'float'>, <type 'complex'>)
根据jonrsharpe的回答,您可以使用正则表达式从每行输入中提取数字字段。
答案 3 :(得分:0)
从字符串中提取数字有一种非常简单且最好的方法。您可以使用以下代码从字符串中提取N个位数。
- 获取整数 -
import re
s = 'flour, 100, grams, 200HC'
print(re.findall('\d+', s))
-Get float Numbers -
import re
map(float, re.findall(r"""\d+ # one or more digits
(?: # followed by...
\. # a decimal point
\d+ # and another set of one or more digits
)? # zero or one times""",
"Numbers like 1.1, 2, 34 and 15.16.",
re.VERBOSE))