Re: replace numbers in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Herron

    Re: replace numbers in a string

    Beema Shafreen wrote:
    hi All,
    >
    i have few lines in file
    "ttccatttctggac atgacgtctgt6901 ggtttaagctttgtg aaagaatgtgctttg attcg"
    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]', '',
    "ttccatttctggac atgacgtctgt6901 ggtttaagctttgtg aaagaatgtgctttg attcg")
    'ttccatttctggac atgacgtctgtggtt taagctttgtgaaag aatgtgctttgattc g'


    Gary Herron


    >
    --
    Beema Shafreen
    ------------------------------------------------------------------------
    >
    --

    >
  • Peter Otten

    #2
    Re: replace numbers in a string

    Gary Herron wrote:
    Beema Shafreen wrote:
    >hi All,
    >>
    >i have few lines in file
    >"ttccatttctgga catgacgtctgt690 1ggtttaagctttgt gaaagaatgtgcttt gattcg"
    >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]', '',
    "ttccatttctggac atgacgtctgt6901 ggtttaagctttgtg aaagaatgtgctttg attcg")
    'ttccatttctggac atgacgtctgtggtt taagctttgtgaaag aatgtgctttgattc g'
    Or use str methods.
    In Python 2.6:
    >>import string
    >>"tctgt6901ggt ttaa".translate (None, string.digits)
    'tctgtggtttaa'

    Older versione:
    >>"tctgt6901ggt ttaa".translate (string.maketra ns("", ""), string.digits)
    'tctgtggtttaa'

    Peter

    Comment

    Working...