我通过从数据库json中检索数据来创建一个包含输入数据的字典文件,并且进展顺利。但它只有一个字,当我试图使用两个单词时,没有运行只读一个单词。
此数据库json
[{"id":"1","kingdom":"Metazoa ","phylum":"Arthropoda ","class":"Insecta ","order":"Hemiptera ","family":"Belostomatidae ","genus":"Abedus"},
{"id":"2","kingdom":"Viridiplantae ","phylum":"Streptophyta ","class":"unclassified_Streptophyta ","order":"Pinales ","family":"Pinaceae ","genus":"Abies"}]
这是我的脚本json
#!/usr/bin/python
import urllib2
import simplejson as json
import optparse
response = urllib2.urlopen("http://localhost/csv/taxo.json")
database = json.load(response)
parser = optparse.OptionParser(usage="usage: %prog [options] genus_name",
version="%prog v.1.0")
parser.add_option("-g",
action="store",
default=False,
help="input genus taxonomy")
options, args = parser.parse_args()
if options.g == 0:
parser.error("wrong option of arguments")
for taxonomy in database:
if taxonomy["genus"] == options.g:
print "Result find taxonomy"
print "Kingdom :", taxonomy['kingdom'],
print "phylum :", taxonomy['phylum'],
print "class :", taxonomy['class'],
print "order :", taxonomy['order'],
print "family :", taxonomy['family'],
print "genus :", taxonomy['genus']
break
else:
print ("No found genus on taxanomy")
示例
myscript.py -g Abedus,Abies
只有一个数据的结果显示" Abedus"," Abies"没有显示