Extract the numeric and alphabetic part from an alphanumeric string

Discussion in 'Python' started by Sandhya Prabhakaran, Aug 3, 2009.

  1. Hi,

    I have a string as str='123ACTGAAC'.

    I need to extract the numeric part from the alphabetic part which I
    did using123

    To get the alphabetic part, I could doACTGAAC
    But when I giveTraceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: expected a character buffer object

    How do I blank out the initial numeric part so as to get just the
    alphabetic part. The string is always in the same format.

    Please help.

    Regards,
    Sandhya
     
    Sandhya Prabhakaran, Aug 3, 2009
    #1
    1. Advertisements

  2. Sandhya Prabhakaran

    Peter Brett Guest

    Firstly, you really should read the Regular Expression HOWTO:

    http://docs.python.org/howto/regex.html#regex-howto

    Secondly, is this what you wanted to do?
    'ACTGAAC'

    Regards,

    Peter
     
    Peter Brett, Aug 3, 2009
    #2
    1. Advertisements

  3. Sandhya Prabhakaran

    Andreas Tawn Guest

    If the format's always the same, you could use slicing instead.
    'ACTGAAC'

    BTW, you should avoid using built-ins like str for variable names. Bad
    things will happen.

    Cheers,

    Drea
     
    Andreas Tawn, Aug 3, 2009
    #3
  4. Sandhya Prabhakaran

    MRAB Guest

    [snip]

    I get:

    ['123']

    which is a _list_ of the strings found.
     
    MRAB, Aug 3, 2009
    #4
  5. The docs for re.findall say that it returns a list of matches. So
    '123' will be numer[0].
    That's what would happen if you pass in a list instead of a string to replace.
     
    Kushal Kumaran, Aug 3, 2009
    #5
  6. <snip>

    Did you really cut&paste that from an interpreter window? I doubt
    it...
    Compare... YOU claim to have gotten an INTEGER (there are no quotes
    around the output value). I get a one element LIST containing a STRING
    value.
    And that format is?

    Given just your example, one could interpret it to be: 3 digits
    followed by 7 alphabetic characters. For that, I'd be using a simple

    nmr = str[:3] #still in character representation
    str = str[3:]

    Or do you mean a variable width integer field followed by a variable
    width alpha field?
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Aug 3, 2009
    #6
  7. Sandhya Prabhakaran

    alex23 Guest

    You shouldn't use 'str' as a label like that, it prevents you from
    using the str() function in the same body of code.
    ('123', 'ACTGAAC')

    If by 'always in the same format' you mean the positions of the
    numbers & alphas,
    you could slightly abuse the struct module:
    ('123', 'ACTGAAC')

    But seriously, you should use slicing:
    ('123', 'CTGAAC')

    You can also label the slices, which can be handy for self-documenting
    your code:
    ('123', 'CTGAAC')
     
    alex23, Aug 3, 2009
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.