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

Était-ce utile?

La 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)

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top