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;
}
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;
}
相关文章推荐
- 关于Python的列表append()返回None对象
- 使用python的列表解析以及函数式计算来简化代码
- python运行shell命令并得到返回结果(比C++方便多了)
- python 中的列表解析和生成表达式
- python 列表解析
- python整理十——生成器表达式与列表解析
- Python中的列表解析和生成表达式
- python 的一些知识点(列表解析、打包、装饰器、type、slots)
- Python 生成器函数,生成器表达式,迭代器,列表解析
- 【转】python 中的列表解析和生成表达式
- Python里有趣的列表解析和生成器表达式
- python 列表解析
- Python列表解析
- C++ 初始化列表 解析
- python 中的列表解析和生成表达式
- python列表解析的新方法
- python 中的列表解析和生成表达式
- 简明python教程 --C++程序员的视角(四):容器类型(字符串、元组、列表、字典)和参考
- [Python入门及进阶笔记]Python-基础-列表及列表解析小结
- Python小练习-自动登录人人发送消息并返回好友列表