如何恢复损坏的,部分腌制的文件? [英] How can I recover a corrupted, partially pickled file?

查看:135
本文介绍了如何恢复损坏的,部分腌制的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆