我如何从一个字符串中删除char或int,其中包含int和char示例,如下所示

时间:2013-12-17 10:27:08

标签: python

我有一根长串“结核分枝杆菌H37Rv | Rv0153c | ptbB

out put应该是这样的:

"370153"

"M.tuberculosisHRv|Rvc|ptbB<b"

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用re.sub

>>> import re
>>> re.sub(r'[0-9]', '', 'M. tuberculosis H37Rv|Rv0153c|ptbB')
'M. tuberculosis HRv|Rvc|ptbB'
>>> re.sub(r'[^0-9]', '', 'M. tuberculosis H37Rv|Rv0153c|ptbB')
'370153'

答案 1 :(得分:0)

试试这个:

In [4]: ''.join([x for x in s if x.isdigit()])
Out[4]: '370153'

最快的方法是:

In [4]: ''.join((x for x in s if x.isdigit()))
Out[4]: '370153'