您的位置:首页 > 编程语言 > C语言/C++

C++解析Python返回的列表

2014-12-25 17:47 204 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: