3 回答
TA贡献1859条经验 获得超6个赞
如果使用shelve,则不必记住对象的腌制顺序,因为shelve它为您提供了类似字典的对象:
搁置您的工作:
import shelve
T='Hiya'
val=[1,2,3]
filename='/tmp/shelve.out'
my_shelf = shelve.open(filename,'n') # 'n' for new
for key in dir():
try:
my_shelf[key] = globals()[key]
except TypeError:
#
# __builtins__, my_shelf, and imported modules can not be shelved.
#
print('ERROR shelving: {0}'.format(key))
my_shelf.close()
恢复:
my_shelf = shelve.open(filename)
for key in my_shelf:
globals()[key]=my_shelf[key]
my_shelf.close()
print(T)
# Hiya
print(val)
# [1, 2, 3]
TA贡献1780条经验 获得超4个赞
坐在这里并未能将其另存globals()为字典,我发现您可以使用莳萝库对会话进行腌制。
可以使用以下方法完成:
import dill #pip install dill --user
filename = 'globalsave.pkl'
dill.dump_session(filename)
# and to load the session again:
dill.load_session(filename)
TA贡献1797条经验 获得超4个赞
这是使用spyderlib函数保存Spyder工作区变量的方法
#%% Load data from .spydata file
from spyderlib.utils.iofuncs import load_dictionary
globals().update(load_dictionary(fpath)[0])
data = load_dictionary(fpath)
#%% Save data to .spydata file
from spyderlib.utils.iofuncs import save_dictionary
def variablesfilter(d):
from spyderlib.widgets.dicteditorutils import globalsfilter
from spyderlib.plugins.variableexplorer import VariableExplorer
from spyderlib.baseconfig import get_conf_path, get_supported_types
data = globals()
settings = VariableExplorer.get_settings()
get_supported_types()
data = globalsfilter(data,
check_all=True,
filters=tuple(get_supported_types()['picklable']),
exclude_private=settings['exclude_private'],
exclude_uppercase=settings['exclude_uppercase'],
exclude_capitalized=settings['exclude_capitalized'],
exclude_unsupported=settings['exclude_unsupported'],
excluded_names=settings['excluded_names']+['settings','In'])
return data
def saveglobals(filename):
data = globalsfiltered()
save_dictionary(data,filename)
#%%
savepath = 'test.spydata'
saveglobals(savepath)
请让我知道这对你有没有用。大卫·BH
添加回答
举报