很难用简短的画描述清楚,问题写在了代码中

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:语句的缩进内

所以也可以排除取不到值的情况

讨论数量: 1
Jason990420

很难看懂代码的内容 !

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)

        for key_date, val_ohlcv in val.items():
...
            except Exception as e:
                break
            whole_dic.setdefault(key, [0, 0, 0, 0, 0])
            whole_abs_dic.setdefault(key, [0, 0, 0, 0, 0])

When exception happened, then the execution of your script will break from the for loop. Maybe it’s why you got KeyError exception when whole_dic[key] called.

通过写入json文件发现写入的内容是一个空字典

It means that the statement whole_dic.setdefault(key, [0, 0, 0, 0, 0]) never been executed !

2周前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!