我正在努力与几个音乐播放器集成.目前,我最喜欢的是exaile.
在新版本中,他们将数据库格式从SQLite3迁移到内部Pickle格式.我想知道是否有办法访问pickle格式文件,而无需手动反向设计格式.
我知道有cPickle python模块,但我不知道它是否可以直接从C调用.
rts*_*ts1 22
有一个名为PicklingTools的库,我帮助维护它可能很有用:它允许你用C++形成数据结构然后你可以pickle/unpickle ...它是C++,而不是C,但这应该不是问题这些天(假设你使用的是gcc/g ++套件).
该库是一个普通的C++库(分发中有C++和Python的例子,展示了如何在C++和Python的套接字和文件上使用库),但总的来说,可以使用文件的基础知识.
基本思想是PicklingTools库为您提供了来自C++的"类似python"的数据结构,以便您可以对Python/C++进行序列化和反序列化.所有(?)基本类型:int,long int,string,None,complex,dictionarys,lists,ordered dictionaries和tuples.很少有钩子来做自定义类,但是这部分有点不成熟:库的其余部分非常稳定并且已经活动了8年(?)年.
简单的例子:
#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;
}
Run Code Online (Sandbox Code Playgroud)
网站上还有其他文档(FAQ和用户指南).
希望这很有用:
Gooday,
任贤齐
小智 5
就像 Cristian 所说的,你可以很容易地在你的 C 代码中嵌入 python 代码,参见这里的例子。
在 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()
Run Code Online (Sandbox Code Playgroud)