2014-10-04 120 views
-2

如何从字符串中提取数字以便能够操纵它?该号码可以是intfloat。例如,如果字符串是"flour, 100, grams""flour, 100.5, grams",则提取数字100100.5如何从Python中的字符串中提取数字?

代码

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' 
+2

你会做同样的方式在Python的任何其他版本。你的琴弦究竟是什么?你期望从他们身上得到什么?你有什么尝试过,但没有工作?它是如何失败的? – 2014-10-04 12:23:29

+0

Martijn的另一半问题?这不是一个代码写入服务。 – jonrsharpe 2014-10-04 12:28:34

回答

3

鉴于你的字符串的结构,当您使用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

你试过的尝试,除了在你的类型强制转换模块将扔掉串粉,但保持100

string = 'flour, 100, grams' 
numbers = [] 

    for i in string.split(','): 
    try: 
     print int(i) 
     numbers.append(i) 
    except: pass 
+0

你可以扩展一下这应该如何实施?它不能被简单地拖入列表理解中。 – jonrsharpe 2014-10-04 12:52:33

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的答案。

0

从字符串中提取数字有一个非常简单和最好的方法。您可以使用以下代码从字符串中提取N位数字。

- 获得整数 -

import re 
s = 'flour, 100, grams, 200HC' 
print(re.findall('\d+', s)) 

-GET浮点数 -

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)) 
相关问题