我正在编写一个代码,该代码从站点中获取一些文本,然后使用for循环获取我感兴趣的文本的一部分。我可以打印此文本,但我想知道如何将其发送到列表中以备后用。到目前为止,我编写的代码就是这个代码。
import urllib2
keyword = raw_input('keyword: ')
URL = "http://www.uniprot.org/uniprot/?sort=score&desc=&compress=no&query=%s&fil=&limit=10&force=no&preview=true&format=fasta" % keyword
filehandle = urllib2.urlopen(URL)
url_text = filehandle.readlines()
for line in url_text:
if line.startswith('>'):
print line[line.index(' ') : line.index('OS')]
只需使用append
:
lines = []
for line in url_text:
if line.startswith('>'):
lines.append(line) # or whatever else you wanted to add to the list
print line[line.index(' ') : line.index('OS')]
编辑:在旁注中,python可以直接在文件上循环-如:
url_text = filehandle.readlines()
for line in url_text:
pass
# can be shortened to:
for line in filehandle:
pass
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句