Connecting Tech Pros Worldwide Help | Site Map

Re: replace numbers in a string

Gary Herron
Guest
 
Posts: n/a
#1: Oct 9 '08
Beema Shafreen wrote:
Quote:
hi All,
>
i have few lines in file
"ttccatttctggacatgacgtctgt6901ggtttaagctttgtgaaaga atgtgctttgattcg"
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:
Quote:
Quote:
Quote:
>>import re
>>re.sub('[0-9]', '',
"ttccatttctggacatgacgtctgt6901ggtttaagctttgtgaaaga atgtgctttgattcg")
'ttccatttctggacatgacgtctgtggtttaagctttgtgaaagaatgt gctttgattcg'


Gary Herron


Quote:
>
--
Beema Shafreen
------------------------------------------------------------------------
>
--
http://mail.python.org/mailman/listinfo/python-list
>
Peter Otten
Guest
 
Posts: n/a
#2: Oct 9 '08

re: Re: replace numbers in a string


Gary Herron wrote:
Quote:
Beema Shafreen wrote:
Quote:
>hi All,
>>
>i have few lines in file
>"ttccatttctggacatgacgtctgt6901ggtttaagctttgtgaaag aatgtgctttgattcg"
>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:
>
>
Quote:
Quote:
>>>import re
>>>re.sub('[0-9]', '',
"ttccatttctggacatgacgtctgt6901ggtttaagctttgtgaaaga atgtgctttgattcg")
'ttccatttctggacatgacgtctgtggtttaagctttgtgaaagaatgt gctttgattcg'
Or use str methods.
In Python 2.6:
Quote:
Quote:
Quote:
>>import string
>>"tctgt6901ggtttaa".translate(None, string.digits)
'tctgtggtttaa'

Older versione:
Quote:
Quote:
Quote:
>>"tctgt6901ggtttaa".translate(string.maketrans("" , ""), string.digits)
'tctgtggtttaa'

Peter
Closed Thread