C++解析Python返回的列表

来源:互联网 发布:贵州卫计委网络直报 编辑:程序博客网 时间:2024/06/06 13:05
python代码:获取Gis环境变量默认值
import arcpy
def get_envs():
    envs = []

    for i in arcpy.ListEnvironments():
        env_name = 'arcpy.env.' + i
        envs.append(env_name + ':' + str(eval_r(env_name)))

    return envs

if __name__ == "__main__":
    get_envs()


C++调用:

int test_get_envs()
{

Py_Initialize();

PyObject* get_envs_module = PyImport_ImportModule("get_envs");
PyObject* get_envs_func = PyObject_GetAttrString(get_envs_module, "get_envs");
PyObject* func_ret_val = PyObject_CallFunction(get_envs_func, NULL);
int list_len = PyObject_Size(func_ret_val);//列表长度40
PyObject *list_item = NULL;//python类型的列表元素
char * str_item = NULL;//c类型的列表元素
for (int i = 0; i < list_len; i++)
{
list_item = PyList_GetItem(func_ret_val, i);//根据下标取出python列表中的元素
str_item = PyString_AsString(list_item);//转换为c类型的数据
printf("%s\n", str_item);
}
Py_Finalize();
return 0;
}
0 0
原创粉丝点击