Question

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

Was it helpful?

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)

OTHER TIPS

You can also do this with a unix utility: tr -d '\n' < seq.fa Just use the option -d 'whitespace-element' that you want to remove.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top