我有一个从api中提取json数据的脚本,我希望它在拉动所述数据之后,解码并选择要存储到db中的标记。现在我只需要让脚本返回特定的调用值。在我尝试解码之前,这就是脚本的样子。
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
这就是我被告知试图让这些特定标签返回
return [{key: value for key, value in result.items() if key in ("twitter_id", "office")} for result in jsonData]
但是会返回错误。有什么建议吗?
'结果':[{' twitter_id':' RepToddYoung',' ocd_id':' ocd-division / country :us / state:in / cd:9',' oc_email':' Rep.Toddyoung@opencongress.org' ;,' middle_name':&#39 ; 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&#39 ;:是的,' title':' Rep',' govtrack_id':' 412428',' crp_id':& #39; N00030670',' term_end':' 2017-01-03',' chamber':' house',&# 39; state_name':' Indiana',' fax':' 202-226-6866',' phone':&#39 ; 202-225-5315',&#3 9;性别':' M',' fec_ids':[' H0IN09070'],'州':' IN& #39;,'网站':' http://toddyoung.house.gov',' name_suffix':无,' icpsr_id':21133,& #39; facebook_id':' 186203844738421',' contact_form':' https://toddyoungforms.house.gov/give-me-your-opinion','生日':& #39; 1972-08-24'},{' twitter_id':' SenDonnelly',' ocd_id':' ocd-division / country :我们/州:在',' oc_email':' Sen.Donnelly@opencongress.org',' middle_name':无,' lis_id& #39;:' S356',' first_name':' Joe',' youtube_id':' sendonnelly',& #39; last_name':' Donnelly',' bioguide_id':' D000607','区':无,'昵称&#39 ;:无,'办公室' 720哈特参议院办公大楼' state_rank':' junior',' ; thomas_id':' 01850',' term_start':' 2013-01-03',' party':' D',' in_off ice':是的,' title':' Sen',' govtrack_id':' 412205',' crp_id&#39 ;:' N00026586',' term_end':' 2019-01-03',' chamber':'参议院' ,' state_name':' Indiana',' fax':' 202-225-6798',' phone': ' 202-224-4814','性别':' M',' senate_class':1,' fec_ids' :[' H4IN02101',' S2IN00091'],'州':' IN',' votesmart_id':34212, '':' http://www.donnelly.senate.gov',' name_suffix':无,' icpsr_id':20717,' facebook_id& #39;:' 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',&# 39; lis_id':' S212',' first_name':&#3 9; Daniel',' youtube_id':' SenatorCoats',' last_name':' Coats',' bioguide_id&#39 ;:' C000542','区':无,'昵称':无,'办公室':' 493罗素参议院办公室建立',' state_rank':' senior',' thomas_id':' 00209',' term_start': ' 2011-01-05',' party':' R',' in_office':是的,'标题' :' Sen',' govtrack_id':' 402675',' crp_id':' N00003845',' term_end':' 2017-01-03',' chamber':' senate',' state_name':' Indiana& #39;,'传真':' 202-228-1820','电话':' 202-224-5623',&# 39;性别':' M',' senate_class':3,' fec_ids':[' S0IN00053'],&#39 ;州':' IN',' votesmart_id':53291,'网站':' http://www.coats.senate.gov',& #39; name_suffix':无,' icpsr_id':14806 ,' facebook_id':' 180671148633644',' contact_form':' http://www.coats.senate.gov/contact/','生日' :' 1943-05-16'}]}
答案 0 :(得分:0)
我猜你的json看起来像你发布的123 -> valid
-123 -> valid
123- -> invalid
-1-12-123 -> invalid
-123-12 -> invalid
。即[{}, {}, {}]
是一个字典的python列表。
试试这个?
grab
此外,如果您希望原始答案有效,则需要添加“其他”值。在将来,我建议发布你正在获得的错误,而不是仅仅说“这引发了错误”。