如何在Python中从字符串中提取数字?

如何在Python中从字符串中提取数字?,python,string,python-3.x,floating-point,int,Python,String,Python 3.x,Floating Point,Int,如何从字符串中提取一个数字以对其进行操作?数字可以是int或float。例如,如果字符串是“面粉,100,克”或“面粉,100.5,克”,则提取数字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/extr

如何从字符串中提取一个数字以对其进行操作?数字可以是
int
float
。例如,如果字符串是
“面粉,100,克”
“面粉,100.5,克”
,则提取数字
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'
输出

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'
回溯(最近一次呼叫最后一次):
文件“/Users/lewis/Documents/extracting numbers.py”,第2行,在
数字=[int(x)表示字符串中的x.split(“,”)]
文件“/Users/lewis/Documents/extracting numbers.py”,第2行,在
数字=[int(x)表示字符串中的x.split(“,”)]
ValueError:基数为10的int()的文本无效:“面粉”

给定字符串的结构,当您使用将字符串拆分为三个字符串的列表时,您应该只使用三个元素中的一个:

>>> s = "flour, 100, grams"
>>> s.split(",")
['flour', ' 100', ' grams']
>>> s.split(",")[1] # index the middle element (Python is zero-based)
' 100'
然后可以使用将该字符串转换为数字:

>>> float(s.split(",")[1])
100.0
如果不能确定字符串的结构,可以使用(正则表达式)提取数字并将其全部转换为:

>>> 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
,然后再转换为
复数
(仅扩展示例)。如果您希望获得/保留最合适的输入类型,则尝试转换的顺序很重要,因为
int
将成功转换为
浮点
,但反之亦然,因此您需要先尝试将输入转换为
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'>)
def将_转换为_编号(n):
候选类型=(int、float、complex)
对于候选类型中的t:
尝试:
返回t(str(n))
除值错误外:
#通过
打印“{!r}不是{}”。格式(n,t)#如果不是调试则注释掉
其他:
raise VALUERROR(“{!r}无法转换为任何:{}”格式(n,候选类型))
>>>s=“面粉,100克”
>>>n=将_转换为_编号(s.split(',')[1])
>>>类型(n)
>>>n
100
>>>s=“面粉,100.123克”
>>>n=将_转换为_编号(s.split(',')[1])
“100.123”不是
>>>类型(n)
>>>n
100.123
>>>n=将_转换为_编号('100+20j'))
“100+20j”不是
“100+20j”不是
>>>类型(n)
>>>n
(100+20j)
>>>n=将_转换为_编号(“一”)
“一”不是
“一”不是
“一”不是
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/tmp/ctn.py”,第10行,转换为编号
raise VALUERROR(“{!r}无法转换为任何:{}”格式(n,候选类型))
ValueError:“一”无法转换为以下任何一种:(,)

您可以根据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))

您可以使用与任何其他Python版本相同的方法来执行此操作。你到底想干什么?你希望从中得到什么?什么东西你自己试过了,但没有成功?它是怎么失败的?还有Martijn的另一半问题?这不是一个代码编写服务。你能详细介绍一下应该如何实现它吗?不能简单地把它放到一个列表中。