很难用简短的画描述清楚,问题写在了代码中
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文件发现写入的内容是一个空字典
很难看懂代码的内容 !
A short excutable program that isolates and demonstrates the problem (Do not paste your massive program, but instead 10-20 lines that clearly show the problem)
When exception happened, then the execution of your script will break from the
for
loop. Maybe it’s why you gotKeyError
exception whenwhole_dic[key]
called.It means that the statement
whole_dic.setdefault(key, [0, 0, 0, 0, 0])
never been executed !