replace numbers in a string
Peter Otten
__peter__ at web.de
Thu Oct 9 05:13:22 EDT 2008
Gary Herron wrote:
> Beema Shafreen wrote:
>> hi All,
>>
>> i have few lines in file
>> "ttccatttctggacatgacgtctgt6901ggtttaagctttgtgaaagaatgtgctttgattcg"
>> i need to replace the number and get only the alphabet in such a case
>> what should i do.
>> Can any body suggest me
>>From the regular expression module, use re.sub like this:
>
>
>>>> import re
>>>> re.sub('[0-9]', '',
> "ttccatttctggacatgacgtctgt6901ggtttaagctttgtgaaagaatgtgctttgattcg")
> 'ttccatttctggacatgacgtctgtggtttaagctttgtgaaagaatgtgctttgattcg'
Or use str methods.
In Python 2.6:
>>> import string
>>> "tctgt6901ggtttaa".translate(None, string.digits)
'tctgtggtttaa'
Older versione:
>>> "tctgt6901ggtttaa".translate(string.maketrans("", ""), string.digits)
'tctgtggtttaa'
Peter
More information about the Python-list
mailing list