如何莳萝(腌)到文件? [英] How to dill (pickle) to file?

查看:112
本文介绍了如何莳萝(腌)到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎有点基本,但无法在互联网上找到我所了解的任何内容.如何存储用莳萝腌制的东西?

The question may seem a little basic, but wasn't able to find anything that I understood in the internet. How do I store something that I pickled with dill?

到目前为止,我已经保存了我的构造(pandas DataFrame,它也包含自定义类):

I have come this far for saving my construct (pandas DataFrame, which also contains custom classes):

import dill
dill_file = open("data/2017-02-10_21:43_resultstatsDF", "wb")
dill_file.write(dill.dumps(resultstatsDF))
dill_file.close()

和阅读

dill_file = open("data/2017-02-10_21:43_resultstatsDF", "rb")
resultstatsDF_out = dill.load(dill_file.read())
dill_file.close()

但是我在阅读时得到了错误

but I when reading I get the error

TypeError: file must have 'read' and 'readline' attributes

我该怎么做?

编辑,以供将来的读者使用:在使用这种方法(使我的DataFrame腌制)一段时间之后,现在我不再这样做了.事实证明,不同的程序版本(包括可能存储在莳萝文件中的对象)可能导致无法恢复已腌制的文件.现在,我确保要保存的所有内容都可以表示为字符串(尽可能有效),实际上是人类可读的字符串.现在,我将数据存储为CSV. CSV单元中的对象可能用JSON格式表示.这样,我可以确保在未来的几个月和几年中,我的文件都可读.即使代码发生更改,我也可以通过解析字符串来重写编码器,并且能够通过手动检查了解CSV.

EDIT for future readers: After having used this approach (to pickle my DataFrame) for while, now I refrain from doing so. As it turns out, different program versions (including objects that might be stored in the dill file) might result in not being able to recover the pickled file. Now I make sure that everything that I want to save, can be expressed as a string (as efficiently as possible) -- actually a human readable string. Now, I store my data as CSV. Objects in CSV-cells might be represented by JSON format. That way I make sure that my files will be readable in the months and years to come. Even if code changes, I am able to rewrite encoders by parsing the strings and I am able to understand the CSV my inspecting it manually.

推荐答案

只要给它不带read的文件即可:

Just give it the file without the read:

resultstatsDF_out = dill.load(dill_file)

您也可以像这样莳萝归档

you can also dill to file like this:

with open("data/2017-02-10_21:43_resultstatsDF", "wb") as dill_file:
    dill.dump(resultstatsDF, dill_file)

所以:

dill.dump(obj, open_file)

直接写入文件.而:

dill.dumps(obj) 

序列化obj,您可以将其写入文件自己.

serializes obj and you can write it to file yourself.

与之类似:

dill.load(open_file)

从文件中读取,并且:

dill.loads(serialized_obj)

从序列化的对象构造一个对象,您可以从文件中读取该对象.

constructs an object form a serialized object, which you could read from a file.

建议使用with语句打开文件.

It is recommended to open a file using the with statement.

这里:

with open(path) as fobj:
    # do somdthing with fobj

具有与以下相同的效果:

has the same effect as:

fobj = open(path)
try:
    # do somdthing with fobj
finally:
    fobj.close()

即使在出现异常的情况下,只要您离开with语句的缩进,文件就会立即关闭.

The file will be closed as soon as you leave the indention of the with statement, even in the case of an exception.

这篇关于如何莳萝(腌)到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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