在 python 中循环遍历 pickle 字典

Looping through a pickle dictionary in python

我正在尝试从 pickle 加载数据 object/file 但是我似乎无法消除错误。

我已尝试设置跟踪以确保我的词典中填充了 pickle 文件中的数据。

fr = open(line, 'rb')
    dictionary = pickle.load(fr)
...
for key, value in dictionary.items()

我希望它能够遍历字典,但我继续收到错误消息:'AttributeError: 'list' object has no attribute 'items'.

我试过:

for key in sorted(dictionary.keys()):

但是并没有解决错误。

编辑: sys.version returns 3.7.3.

我试过使用:

for key in dictionary:

这个returns我的数据作为key: ['Head', '-0.02845094', '0.7953885', '2.586351'] 然而 returns 在 运行 时间出现了一个新错误:

walk[key] = []
TypeError: unhashable type: 'list'

我应该从密钥中提取数据还是有更简单的方法?

我不确定您要做什么,但在您的情况下,这只是因为您的 pickle.load returns 列表并且您正在尝试访问 .items 函数列表的(不存在,项目用于字典)。

您可能想尝试一下:

for i in dictionary: # i is the name I usualy use here, its stands for Item but its way shorter and less painfull to type
    # your loop

我不确定这是否是您期望的结果,但至少应该 运行。