3 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果使用shelve,則不必記住對象的腌制順序,因?yàn)閟helve它為您提供了類似字典的對象:
擱置您的工作:
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()
恢復(fù):
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貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
坐在這里并未能將其另存globals()為字典,我發(fā)現(xiàn)您可以使用蒔蘿庫對會話進(jìn)行腌制。
可以使用以下方法完成:
import dill #pip install dill --user
filename = 'globalsave.pkl'
dill.dump_session(filename)
# and to load the session again:
dill.load_session(filename)

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是使用spyderlib函數(shù)保存Spyder工作區(qū)變量的方法
#%% 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)
請讓我知道這對你有沒有用。大衛(wèi)·BH
添加回答
舉報(bào)