如何使用python访问json文件字典中的键值 [英] How to access key values in a json files dictionaries with python

查看:781
本文介绍了如何使用python访问json文件字典中的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以从api中提取json数据,并希望在提取数据后对其进行解码,然后选择要存储到db中的标签.现在,我只需要获取脚本以返回特定的被调用值即可.这就是脚本的外观,在我尝试对其进行解码之前.

I have a script that pulls json data from an api, and I want it to then after pulling said data, decode and pick which tags to store into a db. Right now I just need to get the script to return specific called upon values. this is what the script looks like, before me trying to decode it.

import requests
def call():
payload = {'apikey':'945e8e8499474b7e8d2bc17d87191bce', 'zip' : '47120'}
bas_url = 'http://congress.api.sunlightfoundation.com/legislators/locate'
r = requests.get(bas_url, params = payload)
grab = r.json()
return grab

'结果':[{'twitter_id':'RepToddYoung','ocd_id': 'ocd-division/country:us/state:in/cd:9','oc_email': 'Rep.Toddyoung@opencongress.org','middle_name':'C.','votesmart_id': 120345,'first_name':'Todd','youtube_id':'RepToddYoung', 'last_name':'Young','bioguide_id':'Y000064','district':9, 昵称":无,办公室":"1007 Longworth House Office Building", 'term_start':'2015-01-06','thomas_id':'02019','party':'R', 'in_office':True,'title':'Rep','govtrack_id':'412428','crp_id': 'N00030670','term_end':'2017-01-03','chamber':'house', 'state_name':'Indiana','fax':'202-226-6866','phone': '202-225-5315','gender':'M','fec_ids':['H0IN09070'],'state': 'IN','website':' http://toddyoung.house.gov ','name_suffix':没有, 'icpsr_id':21133,'facebook_id':'186203844738421','contact_form': ' https://toddyoungforms.house.gov/give-me-your-opinion",生日": '1972-08-24'},{'twitter_id':'SenDonnelly','ocd_id': 'ocd-division/country:us/state:in','oc_email': 'Sen.Donnelly@opencongress.org','middle_name':无,'lis_id': 'S356','first_name':'Joe','youtube_id':'sendonnelly','last_name': 'Donnelly','bioguide_id':'D000607','district':无,'nickname': 无,办公室":"720 Hart参议院办公大楼","state_rank": 'junior','thomas_id':'01850','term_start':'2013-01-03','party': 'D','in_office':True,'title':'Sen','govtrack_id':'412205', 'crp_id':'N00026586','term_end':'2019-01-03','chamber':'参议院', 'state_name':'Indiana','fax':'202-225-6798','phone': '202-224-4814','gender':'M','senate_class':1,'fec_ids': ['H4IN02101','S2IN00091'],'state':'IN','votesmart_id':34212, 'website':' http://www.donnelly.senate.gov ','name_suffix':没有, 'icpsr_id':20717,'facebook_id':'168059529893610','contact_form': ' http://www.donnelly.senate.gov/contact/email-joe",生日": '1955-09-28'},{'twitter_id':'SenDanCoats','ocd_id': 'ocd-division/country:us/state:in','oc_email': 'Sen.Coats@opencongress.org','middle_name':'Ray','lis_id':'S212', 'first_name':'Daniel','youtube_id':'SenatorCoats','last_name': '外套','bioguide_id':'C000542','区域':无,'昵称':无, 'office':'493 Russell Senate Office Building','state_rank': 'senior','thomas_id':'00209','term_start':'2011-01-05','party': 'R','in_office':True,'title':'Sen','govtrack_id':'402675', 'crp_id':'N00003845','term_end':'2017-01-03','chamber':'参议院', 'state_name':'Indiana','fax':'202-228-1820','phone': '202-224-5623','gender':'M','senate_class':3,'fec_ids': ['S0IN00053'],'state':'IN','votesmart_id':53291,'website': ' http://www.coats.senate.gov ','name_suffix':无,'icpsr_id ':14806, 'facebook_id':'180671148633644','contact_form': " http://www.coats.senate.gov/contact/',生日':'1943-05-16'}]}

'results': [{'twitter_id': 'RepToddYoung', 'ocd_id': 'ocd-division/country:us/state:in/cd:9', 'oc_email': 'Rep.Toddyoung@opencongress.org', 'middle_name': 'C.', 'votesmart_id': 120345, 'first_name': 'Todd', 'youtube_id': 'RepToddYoung', 'last_name': 'Young', 'bioguide_id': 'Y000064', 'district': 9, 'nickname': None, 'office': '1007 Longworth House Office Building', 'term_start': '2015-01-06', 'thomas_id': '02019', 'party': 'R', 'in_office': True, 'title': 'Rep', 'govtrack_id': '412428', 'crp_id': 'N00030670', 'term_end': '2017-01-03', 'chamber': 'house', 'state_name': 'Indiana', 'fax': '202-226-6866', 'phone': '202-225-5315', 'gender': 'M', 'fec_ids': ['H0IN09070'], 'state': 'IN', 'website': 'http://toddyoung.house.gov', 'name_suffix': None, 'icpsr_id': 21133, 'facebook_id': '186203844738421', 'contact_form': 'https://toddyoungforms.house.gov/give-me-your-opinion', 'birthday': '1972-08-24'}, {'twitter_id': 'SenDonnelly', 'ocd_id': 'ocd-division/country:us/state:in', 'oc_email': 'Sen.Donnelly@opencongress.org', 'middle_name': None, 'lis_id': 'S356', 'first_name': 'Joe', 'youtube_id': 'sendonnelly', 'last_name': 'Donnelly', 'bioguide_id': 'D000607', 'district': None, 'nickname': None, 'office': '720 Hart Senate Office Building', 'state_rank': 'junior', 'thomas_id': '01850', 'term_start': '2013-01-03', 'party': 'D', 'in_office': True, 'title': 'Sen', 'govtrack_id': '412205', 'crp_id': 'N00026586', 'term_end': '2019-01-03', 'chamber': 'senate', 'state_name': 'Indiana', 'fax': '202-225-6798', 'phone': '202-224-4814', 'gender': 'M', 'senate_class': 1, 'fec_ids': ['H4IN02101', 'S2IN00091'], 'state': 'IN', 'votesmart_id': 34212, 'website': 'http://www.donnelly.senate.gov', 'name_suffix': None, 'icpsr_id': 20717, 'facebook_id': '168059529893610', 'contact_form': 'http://www.donnelly.senate.gov/contact/email-joe', 'birthday': '1955-09-28'}, {'twitter_id': 'SenDanCoats', 'ocd_id': 'ocd-division/country:us/state:in', 'oc_email': 'Sen.Coats@opencongress.org', 'middle_name': 'Ray', 'lis_id': 'S212', 'first_name': 'Daniel', 'youtube_id': 'SenatorCoats', 'last_name': 'Coats', 'bioguide_id': 'C000542', 'district': None, 'nickname': None, 'office': '493 Russell Senate Office Building', 'state_rank': 'senior', 'thomas_id': '00209', 'term_start': '2011-01-05', 'party': 'R', 'in_office': True, 'title': 'Sen', 'govtrack_id': '402675', 'crp_id': 'N00003845', 'term_end': '2017-01-03', 'chamber': 'senate', 'state_name': 'Indiana', 'fax': '202-228-1820', 'phone': '202-224-5623', 'gender': 'M', 'senate_class': 3, 'fec_ids': ['S0IN00053'], 'state': 'IN', 'votesmart_id': 53291, 'website': 'http://www.coats.senate.gov', 'name_suffix': None, 'icpsr_id': 14806, 'facebook_id': '180671148633644', 'contact_form': 'http://www.coats.senate.gov/contact/', 'birthday': '1943-05-16'}]}

那是返回的json数据,我想专门调用IE {'twitter_id': 'RepToddYoung'}, or {'first_name': 'Todd'}

thats the json data returned, i want to specifically call upon IE {'twitter_id': 'RepToddYoung'}, or {'first_name': 'Todd'}

代替我的脚本返回它检索的整个json文件

Instead of my script returning the entire json file that it retrieves

推荐答案

查看返回的数据结构.这是一本包含字典列表的字典.您可以使用'results'键访问列表:

Look at the data structure that you are getting back. It's a dictionary that contains a list of dictionaries. You can access the list using the 'results' key:

l = r.json()['results']

从那里开始,包含您要搜索的项的字典是列表的第一项,因此:

From there the dictionary containing the item you are after is the first item of the list, so:

d = l[0]

可以从字典中检索特定值:

And the specific values can be retrieved from the dictionary:

print(d['twitter_id'])
print(d['first_name'])

您可以简化为:

r.json()['results'][0]['twitter_id']
r.json()['results'][0]['first_name']

可能您将要遍历列表:

for d in r.json()['results']:
    print('{first_name} {last_name}: {twitter_id}'.format(**d))

它将输出:


Todd Young: RepToddYoung
Joe Donnelly: SenDonnelly
Daniel Coats: SenDanCoats

这篇关于如何使用python访问json文件字典中的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆