irwan dwiyanto
irwan dwiyanto

Reputation: 690

Can not enter characters in dictionary with python script

I made a dictionary in python by retrieving data in json format.

[{"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"}]

When I access the data, I am trying to take only the ones in which the value of genus is Abies, but instead I get the error

ValueError : invalid literal for int ( ) with base 10 : 'Abies'

But if I input a numeric value, I get data corresponding to the "id" of the json.

This is my script:

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]

How do I get it display data "id", "kingdom", "phlyum", "class", etc of each data that matches the input for genus?

Upvotes: 1

Views: 66

Answers (2)

smac89
smac89

Reputation: 43226

The error you are getting is because you are trying to convert a string containing non numeric values into an integer. To fix that, remove get = int(str(title))


The data you have is a list. Lists use numeric indexing to access elements at different locations in the list. To print genus value, you have to do:

print data[1]['genus']

Note that this targets the second dictionary in the list. To print the value of genus for the first dictionary, you have to change that 1 to 0


To print the value of each dictionary which contains a value for genus matching title, do this:

for attr_map in data:
    if attr_map['genus'] == title:
        print attr_map

Example run of the program:

>>> import json
>>> buffer = '[{"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"}]'
>>> data = json.loads(buffer)
>>> data
[{u'kingdom': u'Metazoa ', u'family': u'Belostomatidae ', u'class': u'Insecta ', u'id': u'1', u'phylum': u'Arthropoda ', u'genus': u'Abedus', u'order': u'Hemiptera '}, {u'kingdom': u'Viridiplantae ', u'family': u'Pinaceae ', u'class': u'unclassified_Streptophyta ', u'id': u'2', u'phylum': u'Streptophyta ', u'genus': u'Abies', u'order': u'Pinales '}]
>>>
>>> title = raw_input("find taxonom: ")
find taxonom: Abies
>>> for attr_map in data:
...     if attr_map['genus'] == title:
...         print attr_map
... 
{u'kingdom': u'Viridiplantae ', u'family': u'Pinaceae ', u'class': u'unclassified_Streptophyta ', u'id': u'2', u'phylum': u'Streptophyta ', u'genus': u'Abies', u'order': u'Pinales '}

Upvotes: 3

UltraInstinct
UltraInstinct

Reputation: 44464

This will find the first entry that matches:

print next(x for x in data if x["genus"] == title)

Upvotes: 2

Related Questions