|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
'
near_dic = {}
near_abs_dic = {}
for key_code6 in increase6:
val6 = increase6[key_code6]
whole_dic = {}
whole_abs_dic = {}
for key in swift:
val = swift[key]
a = 0
a15, a30, a60, a120 = False, False, False, False
for key_date, val_ohlcv in val.items():
try:
o = val_ohlcv[0] - val6[key_date][0]
h = val_ohlcv[1] - val6[key_date][1]
l = val_ohlcv[2] - val6[key_date][2]
c = val_ohlcv[3] - val6[key_date][3]
v = val_ohlcv[4] - val6[key_date][4]
oo, hh, ll, cc, vv = abs(o), abs(h), abs(l), abs(c), abs(v)
sum_ohlc = o + h + l + c
sum_oohhllcc = oo + hh + ll + cc
except Exception as e:
break
whole_dic.setdefault(key, [0, 0, 0, 0, 0])
whole_abs_dic.setdefault(key, [0, 0, 0, 0, 0])
whole_dic[key][4] += v
whole_abs_dic[key][4] += vv
if a < 15:
a15 = True
for s in range(4):
whole_dic[key][s] += sum_ohlc
whole_abs_dic[key][s] += sum_oohhllcc
elif 15 <= a <= 30:
a30 = True
for s in range(1, 4):
whole_dic[key][s] += sum_ohlc
whole_abs_dic[key][s] += sum_oohhllcc
elif 30 < a <= 60:
a60 = True
for s in range(2, 4):
whole_dic[key][s] += sum_ohlc
whole_abs_dic[key][s] += sum_oohhllcc
elif a > 60:
a120 = True
whole_dic[key][4] += sum_ohlc
whole_abs_dic[key][4] += sum_oohhllcc
a += 1
for index, var in enumerate([a15, a30, a60, a120]):
if var is False:
try:
whole_dic[key][index] = 'None'
whole_abs_dic[key][index] = 'None'
except Exception as e:
print(e)
with open(f'E:/n.json', 'w', encoding=de) as f:
json.dump(whole_dic, f, ensure_ascii=False)
#通过写入json文件发现写入的内容是一个空字典
报错:
whole_dic[key][index] = 'None'
~~~~~~~~~^^^^^
KeyError: 'SH600000'
whole_dic[key][index] = 'None'中的key就是遍历循环中的for key in swift:程序每次循环都会调用whole_dic.setdefault(key, [0, 0, 0, 0, 0])
着保证了key一定会存在,那为什么在whole_dic[key][index] = 'None'中还会报错没有键
声明:key是在for key in swift:语句中产生的,所以key是一定存在的,并且whole_dic[key][index] = 'None'是在for key in swift:语句的缩进内
所以也可以排除取不到值的情况
' |
|