如何恢复损坏的,部分腌制的文件? [英] How can I recover a corrupted, partially pickled file?
问题描述
用dill
将数据(a dict
)序列化到磁盘时,我的程序被杀死了.我现在无法打开部分写入的文件.
My program was killed while serializing data (a dict
) to disk with dill
. I cannot open the partially-written file now.
是否可以部分或全部恢复数据?如果是这样,怎么办?
Is it possible to partially or fully recover the data? If so, how?
这是我尝试过的:
>>> dill.load(open(filename, 'rb'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "lib/python3.4/site-packages/dill/dill.py", line 288, in load
obj = pik.load()
EOFError: Ran out of input
>>>
文件不为空:
>>> os.stat(filename).st_size
31110059
注意:字典中的所有数据均由python内置类型组成.
Note: all data in the dictionary was comprised of python built-in types.
推荐答案
即使是遇到错误,纯Python版本的pickle.Unpickler
也会保持堆栈,因此您至少可以从中得到一些帮助:
The pure-Python version of pickle.Unpickler
keeps a stack around even if it encounters an error, so you can probably get at least something out of it:
import io
import pickle
# Use the pure-Python version, we can't see the internal state of the C version
pickle.Unpickler = pickle._Unpickler
import dill
if __name__ == '__main__':
obj = [1, 2, {3: 4, "5": ('6',)}]
data = dill.dumps(obj)
handle = io.BytesIO(data[:-5]) # cut it off
unpickler = dill.Unpickler(handle)
try:
unpickler.load()
except EOFError:
pass
print(unpickler.stack)
我得到以下输出:
[3, 4, '5', ('6',)]
泡菜数据格式并不复杂.仔细阅读Python模块的源代码,您可能会找到一种钩住所有load_
方法的方法,以为您提供更多信息.
The pickle data format isn't that complicated. Read through the Python module's source code and you can probably find a way to hook all of the load_
methods to give you more information.
这篇关于如何恢复损坏的,部分腌制的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!