删除行更改(换行符)

时间:2017-02-19 22:12:43

标签: python sequences

我想从NCBI数据库中找到的序列中删除行更改。 例如:https://www.ncbi.nlm.nih.gov/protein/148744825?report=fasta

我的目标是直接从网页上复制以进行输入并获得一行来正确处理序列。到现在为止,我必须使用记事本并手动删除所有空格(这真的很烦人)。

我尝试过很多东西:

s = s.replace('\n', '')
s = s.strip()
s = ''.join(s.split())

他们都不适合我。也许问题是我如何做输入。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

这有效:

s='''MATLKEKLIAPVAEEETRIPNNKITVVGVGQVGMACAISILGKSLTDELALVDVLEDKLKGEMMDLQHGS
LFLQTPKIVADKDYSVTANSKIVVVTAGVRQQEGESRLNLVQRNVNVFKFIIPQIVKYSPDCIIIVVSNP
VDILTYVTWKLSGLPKHRVIGSGCNLDSARFRYLMAEKLGIHPSSCHGWILGEHGDSSVAVWSGVNVAGV
SLQELNPEMGTDNDSENWKEVHKMVVESAYEVIKLKGYTNWAIGLSVADLIESMLKNLSRIHPVSTMVKG
MYGIENEVFLSLPCILNARGLTSVINQKLKDEEVAQLKKSADTLWGIQKDLKDL'''

s
Out[69]: 'MATLKEKLIAPVAEEETRIPNNKITVVGVGQVGMACAISILGKSLTDELALVDVLEDKLKGEMMDLQHGS\nLFLQTPKIVADKDYSVTANSKIVVVTAGVRQQEGESRLNLVQRNVNVFKFIIPQIVKYSPDCIIIVVSNP\nVDILTYVTWKLSGLPKHRVIGSGCNLDSARFRYLMAEKLGIHPSSCHGWILGEHGDSSVAVWSGVNVAGV\nSLQELNPEMGTDNDSENWKEVHKMVVESAYEVIKLKGYTNWAIGLSVADLIESMLKNLSRIHPVSTMVKG\nMYGIENEVFLSLPCILNARGLTSVINQKLKDEEVAQLKKSADTLWGIQKDLKDL'

s.split()
Out[70]: 
['MATLKEKLIAPVAEEETRIPNNKITVVGVGQVGMACAISILGKSLTDELALVDVLEDKLKGEMMDLQHGS',
 'LFLQTPKIVADKDYSVTANSKIVVVTAGVRQQEGESRLNLVQRNVNVFKFIIPQIVKYSPDCIIIVVSNP',
 'VDILTYVTWKLSGLPKHRVIGSGCNLDSARFRYLMAEKLGIHPSSCHGWILGEHGDSSVAVWSGVNVAGV',
 'SLQELNPEMGTDNDSENWKEVHKMVVESAYEVIKLKGYTNWAIGLSVADLIESMLKNLSRIHPVSTMVKG',
 'MYGIENEVFLSLPCILNARGLTSVINQKLKDEEVAQLKKSADTLWGIQKDLKDL']

''.join(s.split())
Out[71]: 'MATLKEKLIAPVAEEETRIPNNKITVVGVGQVGMACAISILGKSLTDELALVDVLEDKLKGEMMDLQHGSLFLQTPKIVADKDYSVTANSKIVVVTAGVRQQEGESRLNLVQRNVNVFKFIIPQIVKYSPDCIIIVVSNPVDILTYVTWKLSGLPKHRVIGSGCNLDSARFRYLMAEKLGIHPSSCHGWILGEHGDSSVAVWSGVNVAGVSLQELNPEMGTDNDSENWKEVHKMVVESAYEVIKLKGYTNWAIGLSVADLIESMLKNLSRIHPVSTMVKGMYGIENEVFLSLPCILNARGLTSVINQKLKDEEVAQLKKSADTLWGIQKDLKDL'

不确定您的问题可能是什么。投票结束。

答案 1 :(得分:0)

#!/usr/bin/env python3
import urllib.request
response = urllib.request.urlopen('https://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&save=file&log$=seqview&db=protein&report=fasta&sort=&id=148744825&from=begin&to=end&maxplex=1')
html = str(response.read(), 'utf-8')
lines = html.splitlines()

first_line = lines[0]
rest = "".join(lines[1:])

print("first: %s  rest: %s" % (first_line, rest))