문제

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