动态(动态)构建字典时Python的“键错误”
我在这行代码上得到了错误-动态(动态)构建字典时Python的“键错误”,python,json,dictionary,Python,Json,Dictionary,我在这行代码上得到了错误- result_dict['strat'][k]['name'] = current_comps[0].strip() The error is : Keyerror: 'strat' 我有一条输入线 PERSON1 @@ CAR1 # ENTRY : 0 | EXIT : 0 @@ CAR2 # M1 : YES : 10/01/17 02:00 | M2 : NO : 10/02/16 03:00 | M3 : NO : 05/07/17 11:00 | M4 :
result_dict['strat'][k]['name'] = current_comps[0].strip()
The error is : Keyerror: 'strat'
我有一条输入线
PERSON1 @@ CAR1 # ENTRY : 0 | EXIT : 0 @@ CAR2 # M1 : YES : 10/01/17 02:00 | M2 : NO : 10/02/16 03:00 | M3 : NO : 05/07/17 11:00 | M4 : YES : 01/01/16 03:00 @@ TRUCK # M3 : NO : 03/01/17 03:45 | M23 : NO : 01/01/14 07:00 | M27 : YES : 02/006/18 23:00
我希望解析这个输入以生成下面详细介绍的输出。作为这项工作的一部分,我正在尝试构建一个动态插入键和值的字典。我做这件事有很多问题
我能在这方面请求帮助吗
这是我到目前为止试过的-
# File read
f = open('input_data', 'r')
file_cont = f.read().splitlines()
f.close()
#json template
# Initialize dictionary
result_arr = []
result_dict = {}
k = 0
for item in file_cont:
strat = item.split('@@')
result_dict['Person'] = strat[0].strip()
j = 1
while j < len(strat):
# Split various components of the main line
current_comps = strat[j].split('#')
# Name of strat being parsed
result_dict['strat'][k]['name'] = current_comps[0].strip()
# tfs across the various time frames
tfs = current_comps[1].split('|')
# First travel mode
if current_comps[0].strip() == 'CAR1':
temp_low_arr = tfs[0].split(':')
temp_high_arr = tfs[1].split(':')
result_dict['strat'][k]['Entry'] = temp_low_arr[1].strip()
result_dict['strat'][k]['Exit'] = temp_high_arr[1].strip()
# Second travel mode
elif current_comps[0].strip() == 'CAR2':
z = 0
while z < len(tfs):
# Split components of the sign
sign_comp_car_2 = tfs[z].split(':')
result_dict['strat'][k]['tf'][z]['path'] = sign_comp_ma_cross[0].strip()
result_dict['strat'][k]['tf'][z]['sign'] = sign_comp_ma_cross[1].strip()
result_dict['strat'][k]['tf'][z]['sign_time'] = sign_comp_ma_cross[2].strip()
z += 1
# Third travel mode
elif current_comps[0].strip() == 'CAR3':
b = 0
while b < len(tfs):
# Split components of the sign
sign_car_3 = tfs[z].split(':')
result_dict['strat'][k]['tf'][b]['path'] = sign_all_term[0].strip()
result_dict['strat'][k]['tf'][b]['sign'] = sign_all_term[1].strip()
result_dict['strat'][k]['tf'][b]['sign_time'] = sign_all_term[2].strip()
b += 1
j += 1
k += 1
问题在于,当result_dict['strat'][k]尚未初始化时,尝试在result_dict['strat'][k]中分配['name']字段。在运行for循环之前,字典没有名为strat的键。 现在,您可以执行类似于result_dict['strat']=dict的操作,将对象分配给dict中的该键,但当您使用result_dict['strat'][k]进一步为其下标时,它将首先尝试通过访问result_dict['strat']来解决该问题,并期望得到一个可下标的集合或一个字典作为回报。但是,由于该键还不存在,它会抛出错误 您可以改为初始化默认字典: 从集合导入defaultdict ... resultdict=defaultdict ... 否则,在现有代码中,您可以在进入循环之前在result_dict中初始化dict。result_dict是什么?您的j和k增量与它们各自的最外层循环对齐。你确定你没有缺少一级缩进吗?
[{
"Person":"",
"Transport":[
{
"Name":"CAR1",
"Entry":"0",
"Exit":"0"
},
{
"name":"CAR2:",
"tf":[
{
"path":"M1",
"sign":"YES",
"sign_time":"10/01/17 02:00"
},
{
"path":"M2",
"sign":"NO",
"sign_time":"10/02/16 03:00"
},
{
"path":"M3",
"sign":"NO",
"sign_time":"05/07/17 11:00"
},
{
"path":"M4",
"sign":"YES",
"sign_time":"01/01/16 03:00"
}
]
},
{
"name":"CAR3",
"tf":[
{
"path":"M3",
"sign":"NO",
"sign_time":"03/01/17 03:45"
},
{
"path":"M23",
"sign":"NO",
"sign_time":"01/01/14 07:00"
},
{
"path":"M27",
"sign":"Yes",
"sign_time":"02/006/18 23:00"
}
]
}
]
}]