即使在python关闭之后,也不能在内存中共享任何python对象。但是,您可以使用pickle保存对象的状态(如果库支持它,如果不支持,这个解决方案将不起作用)。pickle是python附带的一个库,可以将大多数对象的状态保存到文件中。在
下面是pickle的一个示例:
要保存状态:import pickle
obj = [1, 2, 3, 4]
f = open("/path/to/the/file/where/the/data/should/be/stored.pickle", 'wb') # you do not have to use the suffix '.pickle' however.
pickle.dump(obj, f)
f.close()
要检索它:
^{pr2}$
或者以你的例子来说,运行一次:from tzwhere import tzwhere
import pickle
f = open("/path/to/the/file", 'wb')
pickle.dump(tzwhere.tzwhere(shapely=True))
f.close()
然后用这个来检索它:import pickle
f = open("/path/to/the/file", 'rb')
tz = pickle.load(f)
f.close()
或者作为一条直线,这样就不会占用太多空间:import pickle;f=open("/path/to/the/file",'rb');tz=pickle.load(f);f.close()
我希望这有帮助
代号lambda
PS:如果你想知道pickle是如何工作的,只要看看the documentation。在