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

有帮助吗?

解决方案

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)

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top