Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关键错误的不确定推理_Python - Fatal编程技术网

Python 关键错误的不确定推理

Python 关键错误的不确定推理,python,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-bc

我不确定我的代码中为什么会出现
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: '{'

错误出现在以下行中:

for key in 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
,除非我们在尝试查找
{
在不包含该字符的dict中。

您能发布一个最小的示例吗?您能发布完整的堆栈跟踪吗?@TomDalton updated!请告诉我您还需要哪些其他信息like@Liondancer您的错误消息引用了第56行;哪一个是第56行?此外,您是否确定“事件”“将始终是每行一个?最后,您是否有任何其他代码修改名称
赋值
”的变量?如果
赋值
已重新分配给JSON有效负载
”{“abc”:123},这将是一个直接的错误“
,其中Python将使用
for
循环开始遍历字符串,因此第一个
键将是
”{'
,第二个键将是
,第三个键将是
'a'
。@ChrisDrost是的,我确信每行有一个事件。第56行是
输出。write(str(json_event[“event”][key])
。我没有任何其他代码可以修改
赋值。如果您感兴趣,以下是完整的代码:
{"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": ""}}