如何从 C 读取 python pickle 数据库/文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1296162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I read a python pickle database/file from C?
提问by Phillip Whelan
I am working on integrating with several music players. At the moment my favorite is exaile.
我正在努力与几个音乐播放器集成。目前我最喜欢的是流放。
In the new version they are migrating the database format from SQLite3 to an internal Pickle format. I wanted to know if there is a way to access pickle format files without having to reverse engineer the format by hand.
在新版本中,他们将数据库格式从 SQLite3 迁移到内部 Pickle 格式。我想知道是否有一种方法可以访问泡菜格式文件,而无需手动对格式进行逆向工程。
I know there is the cPickle python module, but I am unaware if it is callable directly from C.
我知道有 cPickle python 模块,但我不知道它是否可以直接从 C 调用。
回答by rts1
There is a library called the PicklingTools which I help maintain which might be useful: it allows you to form data structures in C++ that you can then pickle/unpickle ... it is C++, not C, but that shouldn't be a problem these days (assuming you are using the gcc/g++ suite).
有一个名为 PicklingTools 的库,我帮助维护它可能很有用:它允许您在 C++ 中形成数据结构,然后您可以pickle/unpickle ...它是 C++,而不是 C,但这应该不是问题这些天(假设您使用的是 gcc/g++ 套件)。
The library is a plain C++ library (there are examples of C++ and Python within the distribution showing how to use the library over sockets and files from both C++ and Python), but in general, the basics of pickling to files is available.
该库是一个普通的 C++ 库(发行版中有 C++ 和 Python 的示例,展示了如何在来自 C++ 和 Python 的套接字和文件上使用该库),但总的来说,可以使用酸洗到文件的基础知识。
The basic idea is that the PicklingTools library gives you "python-like" data structures from C++ so that you can then serialize and deserialize to/from Python/C++. All (?) the basic types: int, long int,string, None, complex, dictionarys, lists, ordered dictionaries and tuples are supported. There are few hooks to do custom classes, but that part is a bit immature: the rest of the library is pretty stable and has been active for 8 (?) years.
基本思想是 PicklingTools 库为您提供来自 C++ 的“类似 python”的数据结构,以便您可以随后对 Python/C++ 进行序列化和反序列化。支持所有 (?) 基本类型:int、long int、string、None、complex、字典、列表、有序字典和元组。很少有钩子可以做自定义类,但这部分有点不成熟:库的其余部分非常稳定,并且已经活跃了 8 (?) 年。
Simple example:
简单的例子:
#include "chooseser.h"
int main()
{
Val a_dict = Tab("{ 'a':1, 'b':[1,2.2,'three'], 'c':None }");
cout << a_dict["b"][0]; // value of 1
// Dump to a file
DumpValToFile(a_dict, "example.p0", SERIALIZE_P0);
// .. from Python, can load the dictionary with pickle.load(file('example.p0'))
// Get the result back
Val result;
LoadValFromFile(result, "example.p0", SERIALIZE_P0);
cout << result << endl;
}
There is further documentation (FAQ and User's Guide) on the web site.
网站上还有更多文档(常见问题解答和用户指南)。
Hope this is useful:
希望这是有用的:
Gooday,
再见,
Richie
里奇
回答by Cristian Ciupitu
You can embed a Python interpreter in a C program, but I think that the easiest solution is to write a Python script that converts "pickles" in another format, e.g. an SQLite database.
您可以在 C 程序中嵌入 Python 解释器,但我认为最简单的解决方案是编写一个 Python 脚本,将“pickles”转换为另一种格式,例如 SQLite 数据库。
回答by hhurtta
Like Cristian told, you can rather easily embed python code in your C code, see the example here.
就像 Cristian 所说的,你可以很容易地在你的 C 代码中嵌入 python 代码,参见这里的例子。
Using cPickle is dead easy as well on python you could use something like:
在 python 上使用 cPickle 也很容易,你可以使用类似的东西:
import cPickle
f = open('dbfile', 'rb')
db = cPickle.load(f)
f.close()
# handle db integration
f = open('dbfile', 'wb')
cPickle.dump(db, f)
f.close()