无论其位置如何,都在字符串中提取数字

时间:2017-09-05 05:26:36

标签: python

我想提取' 2'和' 5'在一个字符串中,无论user_Input是什么,但最多的数字是5.如果user_input不包含' 2'和' 5'我想制作一个空白区域。这是我的编码,我是编程的新手

SELECT HITYPE,
SUM(IF(STATUS = 'OPEN',1,0)) AS OPEN,
SUM(IF(STATUS = 'CLOSED',1,0)) AS CLOSED,
COUNT(STATUS) AS TOTAL
from (
    select SUBSTRING_INDEX(HITYPE, '.', 1) as HITYPE, status
    FROM hazardreport WHERE HITYPE IS NOT NULL AND RISKLEVEL='$risklevel'
    AND DATE(DREPORTED) BETWEEN '$dateone' AND '$datetwo'
) as results
GROUP BY HITYPE
ORDER BY TOTAL DESC

3 个答案:

答案 0 :(得分:1)

这是你打算做的吗?

 no_acc =[] ; other_acc =[]
 for pos in range(len(dice_string)):
    if dice_string[pos] == '2' or dice_string[pos] == '5':
       no_acc.append(dice_string[pos])
    else:
       other_acc.append(dice_string[pos])
 print ''.join(no_acc)
 print ''.join(other_acc)

答案 1 :(得分:1)

无论输入的位数是多少,只需将值2和5提取到另一个字符串中。

a = 6572
print a
b = str(a)
c=""
for i in b:
    if i=='2':
        c = c + i
    if i == '5':
        c = c + i   
print c

答案 2 :(得分:0)

如果从字符串中删除,我们可以使用字符串替换:

a = 12345
print a
b = str(a)
b = b.replace('2', ' ')
b = b.replace('5', ' ')
print b

输出: 12345 1 34