【发布时间】:2016-08-03 03:17:54
【问题描述】:
我在python中通过检索json格式的数据制作了一个字典。
[{"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 : 以 10 为底的 int ( ) 的无效文字:'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”、“kingdom”、“phlyum”、“class”等?
【问题讨论】:
-
int(str(title))不可能是正确的:它说“取标题,将其设为字符串,并将此字符串设为整数”。我想标题不是整数。
标签: python json python-2.7