测试后将字典变量存储在列表中

时间:2017-03-22 17:05:34

标签: python json dictionary

我有一个像这样结构的json:

{   "status":"OK", "copyright":"Copyright (c) 2017 Pro Publica Inc. All Rights Reserved.","results":[
  {
     "member_id": "B001288",
     "total_votes": "100",
     "offset": "0",
     "votes": [
       {
       "member_id": "B001288",
       "chamber": "Senate",
       "congress": "115",
       "session": "1",
       "roll_call": "84",
       "bill": {
         "number": "H.J.Res.57",
         "bill_uri": "https://api.propublica.org/congress/v1/115/bills/hjres57.json",
         "title": "Providing for congressional disapproval under chapter 8 of title 5, United States Code, of the rule submitted by the Department of Education relating to accountability and State plans under the Elementary and Secondary Education Act of 1965.",
         "latest_action": "Message on Senate action sent to the House."
       },
       "description": "A joint resolution providing for congressional disapproval under chapter 8 of title 5, United States Code, of the rule submitted by the Department of Education relating to accountability and State ...",
       "question": "On the Joint Resolution",
       "date": "2017-03-09",
       "time": "12:02:00",
       "position": "No"
     },

有时"账单"参数在那里,有时它是空白的,如:

{
       "member_id": "B001288",
       "chamber": "Senate",
       "congress": "115",
       "session": "1",
       "roll_call": "79",
       "bill": {
       },
       "description": "James Richard Perry, of Texas, to be Secretary of Energy",
       "question": "On the Nomination",
       "date": "2017-03-02",
       "time": "13:46:00",
       "position": "No"
     },

我想访问和存储" bill_uri"在列表中,我可以稍后访问它。我已经通过请求包执行.json()将其处理为python。 print votes_json["results"][0]["votes"][0]["bill"]["bill_uri"]等工作得很好,但是当我这样做时:

bill_urls_2 = []
for n in range(0, len(votes_json["results"][0]["votes"])):
    if votes_json["results"][0]["votes"][n]["bill"]["bill_uri"] in votes_json["results"][0]["votes"][n]:
        bill_urls_2.append(votes_json["results"][0]["votes"][n])["bill"]["bill_uri"]

print bill_urls_2

我收到错误KeyError: 'bill_uri'。我认为if语句的结构有问题,特别是我在字典中寻找的关键字。有人可以提供解释/链接到有关如何使用in查找密钥的说明吗?或者查明我如何使用它的错误?

更新:啊哈!我得到了这个工作:

bill_urls_2 = []

for n in range(0, len(votes_json["results"][0]["votes"])):
    if "bill" in votes_json["results"][0]["votes"][n]:
        if "bill_uri" in votes_json["results"][0]["votes"][n]["bill"]:
            bill_urls_2.append(votes_json["results"][0]["votes"][n]["bill"]["bill_uri"])

print bill_urls_2

感谢所有给我建议的人。

1 个答案:

答案 0 :(得分:0)

这里的错误是因为您正在通过调用该键本身来查找字典中的键。这是一个小例子:

my_dict = {'A': 1, 'B':2, 'C':3}

现在 C 每次都可能存在或不存在于dict中。这就是我如何检查dict中是否存在 C

if 'C' in my_dict:
    print(True)

你在做的是:

if my_dict['C'] in my_dict:
    print(True)

如果找不到以my_dict['C']开头的 C ,则会找不到错误。

您需要做的是:

bill_urls_2 = []
for n in range(0, len(votes_json["results"][0]["votes"])):
    if "bill_uri" in votes_json["results"][0]["votes"][n]:
        bill_urls_2.append(votes_json["results"][0]["votes"][n]["bill"]["bill_uri"])

print bill_urls_2