Search code examples
pythonstringstrip

strip sides of a string in python


I have a list like this:

Tomato4439, >gi|224089052|ref|XP_002308615.1| predicted protein [Populus trichocarpa]

I want to strip the unwanted characters using python so the list would look like: Tomato Populus trichocarpa

I can do the following for the first one:

name = ">Tomato4439"
name = name.strip(">1234567890")
print name
Tomato

However, I am not sure what to do with the second one. Any suggestion would be appreciated.


Solution

  • given:

    s='Tomato4439, >gi|224089052|ref|XP_002308615.1| predicted protein [Populus trichocarpa]'
    

    this:

    s = s.split()
    [s[0].strip('0123456789,'), s[-2].replace('[',''), s[-1].replace(']','')]
    

    will give you

    ['Tomato', 'Populus', 'trichocarpa']
    

    It might be worth investigating regular expressions if you are going to do this frequently and the "rules" might not be that static as regular expressions are much more flexible dealing with the data in that case. For the sample problem you present though, this will work.