删除某些数字 python
Remove certain numbers python
我知道如何删除某些文本,但是当涉及到数字时,它会与其中的其他数字冲突。
def get_team_odds(soup):
for i in soup:
soup_output = (i.get_text())
bleach_characters = soup_output.lstrip("Value")
return(bleach_characters.replace("for",""))
这给出了 0.57 10 0 的输出。
我需要去掉 10 和 0。但是如果我使用 replace
或 strip
。它还从第一个中删除了一些数字。所以如果输出是 10.07,它会给我 .7,因为它去掉了所有的 10 和 0。我如何根据位置或其他任何东西去掉某些数字。
您需要处理输出字符串的逻辑段,而不是像 replace
那样处理字符:
output = ' '.join(x for x in output.split() if x not in ('10', '0'))
这也会标准化空白,所以如果这对您来说不合适,则需要更复杂的东西。
您可以将字符串转换为子字符串列表。例如:
x = '0.57 10 0'
#separate the space delimited string into a list containing substrings
y = x.split(' ')
#you can access an element from your list
print y[0]
#or all of the elements
for i in y:
print i
我知道如何删除某些文本,但是当涉及到数字时,它会与其中的其他数字冲突。
def get_team_odds(soup):
for i in soup:
soup_output = (i.get_text())
bleach_characters = soup_output.lstrip("Value")
return(bleach_characters.replace("for",""))
这给出了 0.57 10 0 的输出。
我需要去掉 10 和 0。但是如果我使用 replace
或 strip
。它还从第一个中删除了一些数字。所以如果输出是 10.07,它会给我 .7,因为它去掉了所有的 10 和 0。我如何根据位置或其他任何东西去掉某些数字。
您需要处理输出字符串的逻辑段,而不是像 replace
那样处理字符:
output = ' '.join(x for x in output.split() if x not in ('10', '0'))
这也会标准化空白,所以如果这对您来说不合适,则需要更复杂的东西。
您可以将字符串转换为子字符串列表。例如:
x = '0.57 10 0'
#separate the space delimited string into a list containing substrings
y = x.split(' ')
#you can access an element from your list
print y[0]
#or all of the elements
for i in y:
print i