在Python脚本中嵌入泡菜(或任意)数据 [英] Embed pickle (or arbitrary) data in python script

查看:82
本文介绍了在Python脚本中嵌入泡菜(或任意)数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中,解释器在遇到以下一行时就会停止

In Perl, the interpreter kind of stops when it encounters a line with

__END__

其中。这通常用于在perl脚本的末尾嵌入任意数据。这样,perl脚本可以获取并存储自身存储的数据,这提供了很好的机会。

in it. This is often used to embed arbitrary data at the end of a perl script. In this way the perl script can fetch and store data that it stores 'in itself', which allows for quite nice opportunities.

在我的情况下,我有一个腌制的对象我想存放在某个地方。虽然我可以很好地使用file.pickle文件,但我正在寻找一种更紧凑的方法(以便更轻松地分发脚本)。

In my case I have a pickled object that I want to store somewhere. While I can use a file.pickle file just fine, I was looking for a more compact approach (to distribute the script more easily).

有没有一种机制可以允许

Is there a mechanism that allows for embedding arbitrary data inside a python script somehow?

推荐答案

如果数据不是特别大(很多K),我会这样做。将其编码('base64')并将其包含在三引号引起来的字符串中,并使用.decode('base64')取回二进制数据,并在其周围进行pickle.loads()调用。

If the data is not particularly large (many K) I would just .encode('base64') it and include that in a triple-quoted string, with .decode('base64') to get back the binary data, and a pickle.loads() call around it.

这篇关于在Python脚本中嵌入泡菜(或任意)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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