如何用Python遍历议员JSON数据并提取指定议员的CID?
如何用Python解析JSON提取特定议员的CID?
需求说明
解析API返回的JSON数据,遍历州议员列表,根据姓氏(lastname)找到对应议员并提取其cid字段。
JSON数据结构
{ "response": { "legislator": [ { "@attributes": { "cid": "N00029147", "firstlast": "Andy Harris", "lastname": "Harris", "party": "R", "office": "MD01", "gender": "M", "first_elected": "2010", "exit_code": "0", "comments": "", "phone": "202-225-5311", "fax": "202-225-0254", "website": "http://harris.house.gov", "webform": "https://harris.house.gov/contact-me/email-me", "congress_office": "1533 Longworth House Office Building", "bioguide_id": "H001052", "votesmart_id": "19157", "feccandid": "H8MD01094", "twitter_id": "RepAndyHarrisMD", "youtube_url": "https://youtube.com/RepAndyHarris", "facebook_id": "AndyHarrisMD", "birthdate": "1957-01-25" } }, { "@attributes": { "cid": "N00025482", "firstlast": "Dutch Ruppersberger", "lastname": "Ruppersberger", "party": "D", "office": "MD02", "gender": "M", "first_elected": "2002", "exit_code": "0", "comments": "", "phone": "202-225-3061", "fax": "202-225-3094", "website": "http://ruppersberger.house.gov", "webform": "http://ruppersberger.house.gov/contact-dutch/email-dutch", "congress_office": "2416 Rayburn House Office Building", "bioguide_id": "R000576", "votesmart_id": "36130", "feccandid": "H2MD02160", "twitter_id": "Call_Me_Dutch", "youtube_url": "https://youtube.com/ruppersberger", "facebook_id": "184756771570504", "birthdate": "1946-01-31" } } ] } }
错误代码分析
你提供的代码存在几个关键问题:
- 错误遍历
finance_response_info["response"]:response是字典,遍历它会得到键名而非议员数组。 - 错误访问议员数组:应通过
finance_response_info["response"]["legislator"]访问,而非直接finance_response_info["legislator"]。 - 错误定位
@attributes:@attributes是每个议员对象内的字典,并非顶层数组,无需[0]索引。 - 代码缩进错误:
if语句后的逻辑未正确缩进。
错误代码:
finance_response_info = json.loads(financeInfo) for v in finance_response_info["response"]: for a in finance_response_info["legislator"]: for b in finance_response_info["@attributes"][0]: if (b["lastname"] == lastName): candidateID = b["cid"]
正确实现代码
方式1:找到第一个匹配的议员CID
import json # 假设financeInfo是API返回的JSON字符串 finance_response_info = json.loads(financeInfo) target_lastname = "Harris" # 替换为目标姓氏 candidate_id = None # 遍历议员列表 for legislator in finance_response_info["response"]["legislator"]: # 获取当前议员的属性字典 attrs = legislator["@attributes"] if attrs["lastname"] == target_lastname: candidate_id = attrs["cid"] break # 找到后立即停止遍历 if candidate_id: print(f"匹配到的CID: {candidate_id}") else: print(f"未找到姓氏为{target_lastname}的议员")
方式2:收集所有同姓议员的CID
如果存在多个同姓议员,可收集所有匹配结果:
import json finance_response_info = json.loads(financeInfo) target_lastname = "Harris" candidate_ids = [] for legislator in finance_response_info["response"]["legislator"]: attrs = legislator["@attributes"] if attrs["lastname"] == target_lastname: candidate_ids.append(attrs["cid"]) if candidate_ids: print(f"匹配到的CID列表: {candidate_ids}") else: print(f"未找到姓氏为{target_lastname}的议员")
关键说明
- 正确定位议员数组:
finance_response_info["response"]["legislator"]是存储所有议员的列表,直接遍历即可。 - 每个议员对象的属性都在
@attributes字典中,直接提取该字典后即可访问lastname和cid字段。 - 根据需求选择停止遍历或收集所有结果,提升效率。
内容的提问来源于stack exchange,提问作者Nikhita
相关产品推荐
相关产品推荐

