[英]How to extract a number from a string in Python?
如何从字符串中提取数字以进行操作? 该数字可以是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'
给定字符串的结构,当您使用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]
您是否尝试过除铸型周围的块以外的其他块,这将扔掉细粉,但保持100
string = 'flour, 100, grams'
numbers = []
for i in string.split(','):
try:
print int(i)
numbers.append(i)
except: pass
给自己写一个转换函数,就像下面的转换函数一样,它首先尝试将其参数转换为int
,然后转换为float
,然后转换为complex
(只是扩展示例)。 如果您希望获取/保留最适合输入的类型,则尝试转换的顺序很重要,因为int
将成功转换为float
,反之则不然,因此您需要尝试将输入转换为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的答案从输入的每一行中提取数字字段。
有一个非常简单和最佳的方法来从字符串中提取数字。 您可以使用以下代码从字符串中提取N个数字。
-获取整数-
import re
s = 'flour, 100, grams, 200HC'
print(re.findall('\d+', s))
-获取浮点数-
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.