我有一根长串“结核分枝杆菌H37Rv | Rv0153c | ptbB
out put应该是这样的:
"370153"
或
"M.tuberculosisHRv|Rvc|ptbB<b"
谢谢
答案 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'