如何将字符串从C++传递给我的Python函数?我想在我的C++应用程序中获取字符串,并在Python语言中解析它
发布于 2012-09-23 09:58:14
首先初始化所有需要的变量。
Py_Initialize();
object main_module = import("__main__");//boost::python objects
object dictionary = main_module.attr("__dict__");运行代码来创建一个变量,并设置一个初始值,然后在python中打印出来。
boost::python::exec("resultStr = 'oldvalue'", dictionary);
PyRun_SimpleString("print resultStr");//new value will reflect in python从c++读取相同的变量。
boost::python::object resultStr = dictionary["resultStr"];//read value from python to c++
std::string &processedScript = extract<std::string>(resultStr);上面的字典对象就像一个共享的地图。您可以从c++设置变量。然后检查来自python的新值。
dictionary["resultStr"] = "new value";//set the variable value
PyRun_SimpleString("print resultStr");//new value will reflect in python祝你编码愉快。谢谢。
https://stackoverflow.com/questions/7081472
复制相似问题