【问题标题】:Python script gives KeyErrorPython 脚本给出 KeyError
【发布时间】:2020-01-20 00:56:34
【问题描述】:

大家好,我正在阅读这个包含我想要的所有字段的文本文件

{"Tables":
[
    {
        "Name": "Event",
        "EventType": "Summary",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
        {
        "Name": "Event",
        "EventType": "Account",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
      {
        "Name": "Event",
        "EventType": "Agent",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
      {
        "Name": "Event",
        "EventType": "Server",
        "Fields": "*",
        "SortBy": "WhenOccurred",
        "SortType": "Date",
        "Last": "01/14/2020 16:02:00 -05:00"
    },
    {
        "Name": "ADUser",
        "Fields": "*",
        "SortBy": "",
        "SortType": "None"
    }
]

}

我正在使用遍历所有字段并将响应输入到 get request json 参数的逻辑

with open('tables.txt') as json_file:
    data = json.load(json_file)
    for p in data['Tables']:
        tableName = p['Name']
        sortBy = p['SortBy']
        lastGot = p['Last']
        fields = p['Fields']
        sortType = p['SortType']
        eventType = p['EventType']
        fileName = ""
        sql = ""

        if (tableName == 'Event'):
            sql = "SELECT " + fields + " FROM " + tableName + " WHERE Event.EventType IN ('" + eventType + "') AND " + tableName + "." + sortBy + " > DateFunc('" + lastGot + "') ORDER BY " + tableName + "." + sortBy + ";"

            fileName = "Event-" + eventType
        else:
            if (sortType == 'None'):
                sql = "SELECT " + fields + " FROM " + tableName + ";";
            else:
                sql = "SELECT " + fields + " FROM " + tableName + " WHERE " + tableName + "." + sortBy + " > DateFunc('" + lastGot + "') ORDER BY " + tableName + "." + sortBy + ";"

            fileName = tableName

event_data = sql
#print(event_data)




r = requests.get(url, headers=headers, json={"script": event_data},
                 verify=verify).json()


reponseobject = r
jsonlist = json.dumps(reponseobject)


print(jsonlist)

但它会抛出一个错误,当我想要的所有字段有任何建议时,我不明白如何纠正它?

 Traceback (most recent call last):
  File "C:/Users/PycharmProjects/Vulnerabilities/main.py", line 31, in <module>
    lastGot = p['Last']
KeyError: 'Last'
{'Name': 'Event', 'EventType': 'Summary', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('Summary') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;
{'Name': 'Event', 'EventType': 'Account', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('Account') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;
{'Name': 'Event', 'EventType': 'Agent', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('Agent') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;
{'Name': 'Event', 'EventType': 'server', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
SELECT * FROM Event WHERE Event.EventType IN ('server') AND Event.WhenOccurred > DateFunc('01/14/2020 16:02:00 -05:00') ORDER BY Event.WhenOccurred;

这是我试图捕捉 p 的结果

{'Name': 'Event', 'EventType': 'Summary', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
{'Name': 'Event', 'EventType': 'Account', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
{'Name': 'Event', 'EventType': 'Agent', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
{'Name': 'Event', 'EventType': 'server', 'Fields': '*', 'SortBy': 'WhenOccurred', 'SortType': 'Date', 'Last': '01/14/2020 16:02:00 -05:00'}
error occurred

【问题讨论】:

  • 看起来不错。我怀疑数据不是你想象的那样。捕获异常并打印p
  • 我同意上述评论。抓住它并尝试打印p
  • 为什么在 if 语句周围有括号?此外,变量和函数名称应遵循lower_case_with_underscores 样式。
  • @PeterWood 感谢您的回复,但您所说的数据不是您想的那样是什么意思?我的意思是“最后”只是应该携带数据应该进入的日期和时间。我隔离了for循环,它打印出文本文件没问题,但仍然出现Keyerror这个问题非常令人难以置信
  • 打印p的结果是什么?编辑问题。 minimal reproducible example

标签: python api get


【解决方案1】:

您的第 5 个字段没有任何 Last 键。

    {
        "Name": "ADUser",
        "Fields": "*",
        "SortBy": "",
        "SortType": "None"
    }

确保您拥有此 Last 密钥。或者,如果您知道它会丢失并且这是预期的行为,那么使用 dict.get 方法来使用默认值。

lastGot = p.get('Last', 'DEFAULT_LAST')

【讨论】:

  • 哇谢谢你不能相信这是问题谢谢但如果我有另一个问题我遇到了AttributeError: 'str' object has no attribute 'read'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多