如何从C读取python pickle数据库/文件?

时间:2022-12-27 22:50:46

I am working on integrating with several music players. At the moment my favorite is exaile.

我正在和几个音乐播放器集成。目前我最喜欢的是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格式。我想知道是否有一种方法可以访问pickle格式文件,而无需手动反向设计格式。

I know there is the cPickle python module, but I am unaware if it is callable directly from C.

我知道有cPickle python模块,但是我不知道它是否可以直接从C调用。

3 个解决方案

#1


17  

http://www.picklingtools.com/

http://www.picklingtools.com/

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的套接字和文件),但是一般来说,pickle到文件的基本知识是可用的。

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、dictionarys、list、ordered dictionary和tuple。有几个钩子可以做定制类,但这部分有点不成熟:库的其余部分相当稳定,并且已经活跃了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.

在网站上有更多的文档(FAQ和用户指南)。

Hope this is useful:

希望这是有用的:

Gooday,

Gooday,

Richie

里奇

http://www.picklingtools.com/

http://www.picklingtools.com/

#2


3  

Like Cristian told, you can rather easily embed python code in your C code, see the example here: http://docs.python.org/extending/extending.html#calling-python-functions-from-c

就像克里斯蒂安说的,你可以很容易地将python代码嵌入到你的C代码中,参见这里的例子:http://docs.python.org/extending/extending.html# callingpythonfunctions-from-c。

Using cPickle is dead easy as well on python you could use somehting like:

在python中使用cPickle也很容易,您可以使用一些类似的方法:

import cPickle

f = open('dbfile', 'rb')
db = cPicle.load(f)
f.close()
# handle db integration
f = open('dbfile', 'wb')
cPickle.dump(db, f)
f.close()

#3


1  

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脚本,该脚本可以将“pickle”转换成另一种格式,例如SQLite数据库。

#1


17  

http://www.picklingtools.com/

http://www.picklingtools.com/

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的套接字和文件),但是一般来说,pickle到文件的基本知识是可用的。

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、dictionarys、list、ordered dictionary和tuple。有几个钩子可以做定制类,但这部分有点不成熟:库的其余部分相当稳定,并且已经活跃了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.

在网站上有更多的文档(FAQ和用户指南)。

Hope this is useful:

希望这是有用的:

Gooday,

Gooday,

Richie

里奇

http://www.picklingtools.com/

http://www.picklingtools.com/

#2


3  

Like Cristian told, you can rather easily embed python code in your C code, see the example here: http://docs.python.org/extending/extending.html#calling-python-functions-from-c

就像克里斯蒂安说的,你可以很容易地将python代码嵌入到你的C代码中,参见这里的例子:http://docs.python.org/extending/extending.html# callingpythonfunctions-from-c。

Using cPickle is dead easy as well on python you could use somehting like:

在python中使用cPickle也很容易,您可以使用一些类似的方法:

import cPickle

f = open('dbfile', 'rb')
db = cPicle.load(f)
f.close()
# handle db integration
f = open('dbfile', 'wb')
cPickle.dump(db, f)
f.close()

#3


1  

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脚本,该脚本可以将“pickle”转换成另一种格式,例如SQLite数据库。