Search code examples
pythonwhitespacefasta

strip whitespace except spaces


I find myself with a unique problem. I need to parse a file in which I need to put all of the lines into one string, with which I generally approach with str.strip() however I have realized that there are spaces at the beginning and end of each line which have important value. Is there an easy way to strip all whitespace except spaces? If not, what's a good way to delete specific characters from my strings since an alternate approach would be to do this for all the types of whitespace that appear.

Here's the file in question. http://www.rcsb.org/pdb/files/ss.txt


Solution

  • Use:

    whitespace = "\r\n\t"
    my_string.strip(whitespace)
    

    Or, using the string module:

    import string
    whitespace_except_space = string.whitespace.replace(" ", "")
    my_string.strip(whitespace_except_space)