http://www.picklingtools.com/ http://www.picklingtools.com/
有一个名为 PicklingTools 的库,我帮助维护它,它可能很有用:它允许您在 C++ 中形成数据结构,然后您可以 pickle/unpickle ...它是 C++,而不是 C,但这应该不是问题这些天(假设您正在使用 gcc/g++ 套件)。
该库是一个普通的 C++ 库(发行版中有 C++ 和 Python 的示例,展示了如何通过 C++ 和 Python 的套接字和文件使用该库),但一般来说,pickling 到文件的基础知识是可用的。
基本思想是 PicklingTools 库为您提供来自 C++ 的“类似 python”的数据结构,以便您可以在 Python/C++ 之间进行序列化和反序列化。支持所有(?)基本类型:int、long int、string、None、complex、字典、列表、有序字典和元组。自定义类的钩子很少,但这部分有点不成熟:库的其余部分非常稳定,并且已经活跃了 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;
}
网站上有更多文档(常见问题解答和用户指南)。
希望这有用:
Gooday,
Richie
http://www.picklingtools.com/ http://www.picklingtools.com/