【发布时间】:2017-12-09 08:31:40
【问题描述】:
我已经运行了 5,693 次回归并希望保存输出,因为它需要几个小时才能运行。我已经在一个名为res 的列表中捕获了它,并且该对象(如果重要的话)是来自包statsmodels 的MarkovRegressionResultsWrapper 对象。
我认为要走的路是泡菜。我保存到一个私有目录供我自己使用,所以安全性不是问题,而且 JSON 似乎不适用于对象(我是新手,所以也许这是错误的?)。
这是一个我发现可以正常工作的示例:
import pickle
a = ['test value','test value 2','test value 3']
file_Name = "testfile"
# open the file for writing
fileObject = open(file_Name,'wb')
# this writes the object a to the
# file named 'testfile'
pickle.dump(a,fileObject)
# here we close the fileObject
fileObject.close()
但是,当我使用完全相同的代码,但保存我的列表 res 时,它会给出错误:
file_Name = "testfile"
# open the file for writing
fileObject = open(file_Name,'wb')
# this writes the object a to the
# file named 'testfile'
pickle.dump(res,fileObject)
# here we close the fileObject
fileObject.close()
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-43-ab4800ac1a51> in <module>()
7 # this writes the object a to the
8 # file named 'testfile'
----> 9 pickle.dump(res,fileObject)
10
11 # here we close the fileObject
OSError: [Errno 22] Invalid argument
我在 Macbook Pro 上使用 Python 3.6 和 Jupyter Notebook。 a 和 res 都是列表类型,所以唯一不同的是列表包含的内容。为什么我会收到此错误?这是保存此对象列表的最佳方式还是我应该做一些不同的事情?
【问题讨论】:
-
您的
pickle.dump中的res是什么? -
res是一个包含 5,693 个MarkovRegressionResultsWrapper对象的列表 -
OSError表明这里的问题不一定是您的 Python 代码,但这是一条系统错误消息 (doc)。您正在编写的文件有多大? -
我相信你得到的错误是 known bug in
pickle关于传入对象的大小。 -
@patrick 是的,它只是 MAC。 Windows 和 Linux 用户应该不受影响。
标签: python json pickle statsmodels