我通過檢索json格式的數據在python中創建了一本字典。無法在字典中使用python腳本輸入字符
[{"id":"1","kingdom":"Metazoa ","phylum":"Arthropoda ","class":"Insecta ","order":"Hemiptera ","family":"Belostomatidae ","genus":"Abedus"},<br>
{"id":"2","kingdom":"Viridiplantae ","phylum":"Streptophyta ","class":"unclassified_Streptophyta ","order":"Pinales ","family":"Pinaceae ","genus":"Abies"}]
當我訪問這些數據,我想只拿這樣的,其中的genus
值爲Abies
,而是我得到的錯誤
ValueError : invalid literal for int () with base 10 : 'Abies'
但是,如果我輸入一個數值,我得到的數據對應於json的「id」。
這是我的腳本:
import urllib2
import simplejson
title = raw_input("find taxonom: ")
print "key: ",title
response = urllib2.urlopen("http://localhost/csv/taxo.json")
data = simplejson.load(response)
get = int(str(title))
print data[get]
我如何得到它顯示的數據「ID」,「王國」,每一個爲屬匹配輸入數據的「phlyum」,「階級」,等等?
'int(str(title))'不能正確:它表示「獲取標題,使其成爲一個字符串,並使該字符串的整數」。我認爲標題不是整數。 – 9000