python 2.7 - Adding for-loop elements to a list -
i'm writing code fetches text site , then, for-loop take part of text of interest. can print text know how can send list latter use. far code i've wrote one.
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() line in url_text: if line.startswith('>'): print line[line.index(' ') : line.index('os')]
just use append:
lines = [] line in url_text: if line.startswith('>'): lines.append(line) # or whatever else wanted add list print line[line.index(' ') : line.index('os')] edit: on side note, python can loop directly on file - in:
url_text = filehandle.readlines() line in url_text: pass # can shortened to: line in filehandle: pass
Comments
Post a Comment