不确定KeyError的推理

时间:2015-04-13 21:07:11

标签: python

我不确定为什么我的代码中会出现KeyError。我一直试图解决这个问题,但我陷入困境,而且我已经盯着屏幕看了太久。

在hiev_expected_testy.out文件夹中容纳此事件

事件:

{"marker": {"event_id": "c8d68b0f-75ab-4973-bc60-07f17769e249", "table": "jabba_events", "db": "idea-jabba-applicationData", "time_uuid": "e60cfa50-a98c-44f9-bc6e-68f772acdf88"}, "event": {"experiment_id": "6b8d51b7-51f9-4308-a7c9-2c0b665bb869", "user_id": "a1e971d1-afd2-4ed0-bd06-6ccfc21cfd65", "event_type": "BINARY_ACTION", "event_name": "FileIOSave", "event_payload": "sku=Home,release=2014.11.14.508,FileFormat=Container,TimeInMilliSeconds=5663,PrinterReturnId={83D02DEA-E804-4780-9D83-E2400A7D8AF7},", "bucket": "red", "time_uuid": "e60cfa50-a98c-44f9-bc6e-68f772acdf88", "experiment_name": "Container", "trinity_timestamp": 1428947274912, "context": "PROD", "application_name": "CTG", "jabba_timestamp": null, "event_value": ""}}

我的代码将加载此事件并执行代码中的逻辑并捕获KeyError

e:    
    '{'

通过此活动,程序可以正常运行

事件:

{"marker": {"event_id": "9ca44622-33cb-43fa-94fc-9fa4af77c86e", "table": "jabba_assignments", "db": "idea-jabba-applicationData", "time_uuid": "88009235-6da2-4bc6-b899-73f1d47a6734"}, "event": {"context": "PROD", "put_assignment": "true", "segmentation_profile": "{isNewCompany=false}", "experiment_id": "38c7ef58-62ea-434c-a4b8-30dcbab54282", "bucket": "B", "user_id": 769200395, "assignment_status": "EXISTING_ASSIGNMENT", "experiment_name": "ty14_1068", "page_name": "TTOInit", "trinity_timestamp": 1428947323686, "time_uuid": "88009235-6da2-4bc6-b899-73f1d47a6734", "ignore_sampling_percent": "false", "application_name": "GBODev", "jabba_timestamp": 1428947355740, "create_assignment": "false"}}

没有try catch这就是追溯

Traceback (most recent call last):
  File "/Users/bli1/Development/QE/TrinityTestFramework/poc/tests/out/hive_expected.py", line 56, in <module>
    output.write(str(json_event["event"][key]))
KeyError: '{'

您可以在我的intellij口译员中看到key的值为{

enter image description here

代码:

with open("hive_expected_testy.out", "r") as f:
    with open("mans.out", "w") as output:
        for event in f:
            try:
                json_event = json.loads(event)
                # print(len(json_event["event"]))
                # for i in json_event:
                table = json_event["marker"]["table"]
                if table == "jabba_assignments":
                    for key in assignment:
                        # pdb.set_trace()
                        output.write(str(json_event["event"][key]))
                        output.write("\t")
                    output.write("\n")
                if table == "jabba_events":
                    for key in event:
                        output.write(str(json_event["event"][key]))
                        output.write("\t")
                    output.write("\n")
                if table is None:
                    output.write("No table assigned: {}".format(json_event))
                    output.write("\n")
            except KeyError as e:
                print(event)
                print(e)

assignment = [
    "trinity_timestamp",
    "user_id",
    "application_name",
    "experiment_name",
    "context",
    "create_assignment",
    "put_assignment",
    "ignore_sampling_percent",
    "segmentation_profile",
    "experiment_id",
    "page_name",
    "assignment_status",
    "bucket",
    "time_uuid",
    "jabba_timestamp"
]

event = [
    "trinity_timestamp",
    "application_name ",
    "experiment_name ",
    "user_id",
    "bucket",
    "jabba_timestamp",
    "event_type",
    "event_name",
    "event_payload",
    "time_uuid ",
    "experiment_id",
    "event_value",
    "context"
]

1 个答案:

答案 0 :(得分:4)

错误如下:

for key in event:

由于event在此上下文中绑定到字符串

{"marker": {"event_id": "c8d68b0f-75ab-4973-bc60-07f17769e249", "table": "jabba_events", "db": "idea-jabba-applicationData", "time_uuid": "e60cfa50-a98c-44f9-bc6e-68f772acdf88"}, "event": {"experiment_id": "6b8d51b7-51f9-4308-a7c9-2c0b665bb869", "user_id": "a1e971d1-afd2-4ed0-bd06-6ccfc21cfd65", "event_type": "BINARY_ACTION", "event_name": "FileIOSave", "event_payload": "sku=Home,release=2014.11.14.508,FileFormat=Container,TimeInMilliSeconds=5663,PrinterReturnId={83D02DEA-E804-4780-9D83-E2400A7D8AF7},", "bucket": "red", "time_uuid": "e60cfa50-a98c-44f9-bc6e-68f772acdf88", "experiment_name": "Container", "trinity_timestamp": 1428947274912, "context": "PROD", "application_name": "CTG", "jabba_timestamp": null, "event_value": ""}}

,我们先按顺序查看其中的字符{,然后是",然后是m,除非我们在尝试查找KeyError时停止{ {1}}在不包含该字符的字典中。