如何在 Ipython 控制台中 load/view 腌制对象的结构? (Windows 7,Spyder,Ipython 控制台)
How to load/view structure of pickled object in Ipython console ? (Windows 7, Spyder, Ipython console)
我正在学习其他程序员编写的程序。所以我想查看腌制项目的结构。
由于我需要知道 pickle 数据的结构,我正在尝试使用 Spyder 在 Ipython 中加载 pickle ...例如:
import pickle
data1 = {'a': [1, 2.0, 3, 4+6j],
'b': ('string', u'Unicode string'),
'c': None}
selfref_list = [1, 2, 3]
#selfref_list.append(selfref_list)
output = open('data.pkl', 'wb')
# Pickle dictionary using protocol 0.
pickle.dump(data1, output)
# Pickle the list using the highest protocol available.
pickle.dump(selfref_list, output, -1)
output.close()
我想知道这里 pickled 的 .pkl 文件的结构。
不清楚您所说的 structure
是什么意思。如果我 运行 你的代码,我可以这样做:
In [6]: with open('data.pkl','rb') as f:
...: x = pickle.load(f)
...: y = pickle.load(f)
...:
...:
In [7]: x
Out[7]: {'a': [1, 2.0, 3, (4+6j)], 'b': ('string', 'Unicode string'), 'c': None}
In [8]: y
Out[8]: [1, 2, 3]
我可以用相同数量的读取来恢复你的连续写入。如果我尝试获得更多,我会得到 EOFError: Ran out of input
.
你想知道什么?文件中有多少对象?每个对象的结构? Python 对象和文件字节之间的转换?
当我们不知道有多少物品被腌制时该怎么办...
找到答案:
unpickled_store = []
file_id = open('data.pkl','rb')
while True:
try:
unpickled_item = pickle.load(file_id)
unpickled_store.append(unpickled_item)
except EOFError:
break
我正在学习其他程序员编写的程序。所以我想查看腌制项目的结构。 由于我需要知道 pickle 数据的结构,我正在尝试使用 Spyder 在 Ipython 中加载 pickle ...例如:
import pickle
data1 = {'a': [1, 2.0, 3, 4+6j],
'b': ('string', u'Unicode string'),
'c': None}
selfref_list = [1, 2, 3]
#selfref_list.append(selfref_list)
output = open('data.pkl', 'wb')
# Pickle dictionary using protocol 0.
pickle.dump(data1, output)
# Pickle the list using the highest protocol available.
pickle.dump(selfref_list, output, -1)
output.close()
我想知道这里 pickled 的 .pkl 文件的结构。
不清楚您所说的 structure
是什么意思。如果我 运行 你的代码,我可以这样做:
In [6]: with open('data.pkl','rb') as f:
...: x = pickle.load(f)
...: y = pickle.load(f)
...:
...:
In [7]: x
Out[7]: {'a': [1, 2.0, 3, (4+6j)], 'b': ('string', 'Unicode string'), 'c': None}
In [8]: y
Out[8]: [1, 2, 3]
我可以用相同数量的读取来恢复你的连续写入。如果我尝试获得更多,我会得到 EOFError: Ran out of input
.
你想知道什么?文件中有多少对象?每个对象的结构? Python 对象和文件字节之间的转换?
当我们不知道有多少物品被腌制时该怎么办...
找到答案:
unpickled_store = []
file_id = open('data.pkl','rb')
while True:
try:
unpickled_item = pickle.load(file_id)
unpickled_store.append(unpickled_item)
except EOFError:
break