【发布时间】:2021-02-12 08:30:04
【问题描述】:
我在使用 gzip 和 pickle 时遇到了一些问题。基本上我有以下代码,我尝试使用 gzip 和 pickle.dump 保存随机数据集
import pickle
import gzip
import torch
import numpy as np
with gzip.open('test.pt', "wb") as f:
for d in range(50):
a = np.random.rand(3,2).astype(np.float32)
aa = torch.from_numpy(a)
pickle.dump({'name': str(d), 'person': 'test', 'text1':'blah',
'text2': 'blah', 'data': aa}, f)
with gzip.open('test.pt', "rb") as f:
data4 = pickle.load(f)
print(data4)
只打印第一个元素,为什么?
我期望上面一行的输出是:
[{'name': '0', 'person': 'test', 'text1': 'blah', 'text2': 'blah',
'data': tensor([[0.8789, 0.4588],
[0.0728, 0.6768],
[0.9147, 0.2786]])},
{'name': '1', 'person': 'test', 'text1': 'blah', 'text2': 'blah',
'data': tensor([[0.8789, 0.4588],
[0.0728, 0.6768],
[0.9147, 0.2786]])},
{'name': '2', 'person': 'test', 'text1': 'blah', 'text2': 'blah',
'data': tensor([[0.8789, 0.4588],
[0.0728, 0.6768],
[0.9147, 0.2786]])},
...,
{'name': '49', 'person': 'test', 'text1': 'blah', 'text2': 'blah',
'data': tensor([[0.8789, 0.4588],
[0.0728, 0.6768],
[0.9147, 0.2786]])}]
for d in data4:
print(d)
# prints: name, person, text1, text2, data, why ???**
我的输出除外:
{'name': '0', 'person': 'test', 'text1': 'blah', 'text2': 'blah', 'data': tensor([[0.8789, 0.4588],[0.0728, 0.6768],[0.9147, 0.2786]])}
{'name': '1', 'person': 'test', 'text1': 'blah', 'text2': 'blah', 'data': tensor([[0.8789, 0.4588],[0.0728, 0.6768],[0.9147, 0.2786]])}
{'name': '2', 'person': 'test', 'text1': 'blah', 'text2': 'blah', 'data': tensor([[0.8789, 0.4588],[0.0728, 0.6768],[0.9147, 0.2786]])}
...
{'name': '49', 'person': 'test', 'text1': 'blah', 'text2': 'blah', 'data': tensor([[0.8789, 0.4588],[0.0728, 0.6768],[0.9147, 0.2786]])}
当我这样做时,
for d in data4:
print(d['name'])
我明白了:
TypeError Traceback (most recent call last)
<ipython-input-25-13370f7fdb11> in <module>
22
23 for d in data4:
---> 24 print(d['name'])
TypeError: string indices must be integers**
最后真的不明白为什么我无法使用 d['name'] 访问
非常感谢任何帮助!
【问题讨论】: