我正在尝试用python编写一个程序,它的工作方式几乎就像R's save一样。保存您的工作区的图像。以下是我的代码,但它运行不流畅,请帮助我使它工作
我使用了以下示例数据和一个图,以便可以在其上进行测试
Attempt1 -Using工具架,字典式的工作区节省
import shelve
import numpy as np
import matplotlib.pyplot as plt
import pickle
import os
import pandas as pd
x = np.arange(-3, 3, 0.01)
y = np.sin(np.pi*x)
fig = plt.figure()
ax = fig.add_subplot(111)
line=ax.plot(x, y)
#shelving or pickling my session
my_shelf = shelve.open('shelve.out','c') # 'n' for new
for name in dir():
if not name.startswith (('__','_','In','Out','exit','quit','get_ipython')):
try:
my_shelf[name] = globals()[name] # I didn't undersatnd why to use globals()
except Exception:
pass
print('ERROR shelving: {0}'.format(name))
my_shelf.close()要恢复,请执行以下操作:
my_shelf = shelve.open('shelve.out','r')
for key in my_shelf:
globals()[key]=my_shelf[key]
my_shelf.close()这次用Pickle再试一次:
with open('save.p', 'wb') as f:
for name in dir():
if not name.startswith (('__','_','In','Out','exit','quit','get_ipython')):
try:
pickle.dump(name, f)
except Exception:
print('ERROR shelving: {0}'.format(name))
pass
with open('save.p',"rb") as f: # Python 3: open(..., 'rb')
pickle.load(f) 我将高度感谢任何帮助,道歉的任何错误缩进,同时粘贴到溢出他们得到的变化
发布于 2020-01-08 22:31:15
dill模块提供了此功能。
dump_session(filename='/tmp/session.pkl',main=None,byref=False,**kwds)
load_session(filename='/tmp/session.pkl',main=None,**kwds)
https://stackoverflow.com/questions/42478495
复制相似问题