• C++调用python(C++)


    C++源代码:python部分就是正常的python代码

     1 #include    <string.h>
     2 #include    <math.h>
     3 #include    "iostream"
     4 #include    "string"
     5 #include    "fstream"
     6 #include    <stdio.h>
     7 #include    <stdlib.h>
     8 #include    <sys/time.h>
     9 #include    <unistd.h>
    10 #include    "/usr/include/python2.6/Python.h"
    11 using namespace std;
    12 
    13 #define DEBUG_COUT 1
    14 
    15 string str_tmp[5000][5000];
    16 int list_len = 0;
    17 int val_num = 0;
    18 
    19 int get_py_list(string py_file,string py_path)
    20 {
    21     string sys_path = "sys.path.append('"+py_path+"')";//系统搜索路径添加目标代码路径
    22     
    23     Py_Initialize();//python编译器初始化,无返回值来判断是否初始化成功。需要配合Py_IsInitialized来确认
    24     
    25     if(!Py_IsInitialized())
    26     {
    27         cout<<"初始化python解释器失败,请确认python解析器是否正常安装!!!"<<endl;
    28         return 029     }
    30 
    31     PyRun_SimpleString("import sys");//调用python语句,实际上是一个宏,执行一段Python代码。
    32     PyRun_SimpleString(sys_path.c_str());//添加python可执行路径
    33     
    34     //PyObject* modulename = Py_BuildValue("s", "py_file.c_str()"); //将C++类型转换为python类型。s表示python字符串
    35     //PyObject* module = PyImport_Import(modulename); //返回值为NULL表示调用失败。这两行等同于下面这句
    36     PyObject* get_envs_module = PyImport_ImportModule(py_file.c_str());//导入一个Python模块,参数name可以是*.py文件的文件名。类似Python内建函数import。
    37     
    38     PyObject* get_envs_func = PyObject_GetAttrString(get_envs_module, "readFileFunc");//返回模块对象o中的attr_name 属性或函数,相当于Python中表达式语句o.attr_name
    39     
    40     PyObject* args = Py_BuildValue("(iiss)", 5, 0,"1219.txt", "Outagesked::天津");
    41     PyObject* func_ret_val = PyObject_CallObject(get_envs_func, NULL); //此函数有两个参数,而且都是Python对象指针,其中pfunc是要调用的Python 函数,一般说来可以使用PyObject_GetAttrString()获得。
    42     //第二个参数pargs是函数的参数列表,通常是使用Py_BuildValue()来构建。
    43 
    44     PyObject *list_item = NULL; //python类型的列表元素
    45 
    46     list_len = PyList_Size(func_ret_val); //获取列表长度
    47     #ifdef DEBUG_COUT
    48         cout<<"list_len is "<<list_len<<endl;
    49     #endif
    50     
    51     for (int i = 0; i < list_len; i++)
    52     {
    53         PyObject *list_item = PyList_GetItem(func_ret_val, i);//根据下标i取出python列表中的元素
    54 
    55         val_num = PyList_Size(list_item); //List对象子元素的大小,这里NumOfItems = 3
    56         #ifdef DEBUG_COUT
    57             cout<<" val_num is "<<val_num<<endl;
    58         #endif
    59 
    60         for(int Index_k = 0; Index_k < val_num; Index_k++)
    61         {
    62             PyObject *list_item_sub = PyList_GetItem(list_item, Index_k);//遍历List对象中子元素中的每个元素
    63             str_tmp[i][Index_k] = PyString_AsString(list_item_sub);//转换为c类型的数据
    64             //cout<<str_tmp[i][Index_k]<<endl;
    65             Py_DECREF(list_item_sub); //释放空间
    66         }
    67         Py_DECREF(list_item); //释放python创建对象的引用计数。否则容易引起内存泄漏
    68         if(i == list_len-1)
    69         {
    70             Py_Finalize(); //用于关闭Python解释器,释放解释器所占用的资源
    71             return 0;
    72         }
    73     }
    74     Py_Finalize(); //用于关闭Python解释器,释放解释器所占用的资源
    75     return 0;
    76 }
    77 
    78 int main(int argc,char **argv)
    79 {
    80     string py_file = "readFile";
    81     string py_path = "/root/cpp_make/py_dir";
    82 
    83     get_py_list(py_file,py_path);
    84     
    85     #ifdef DEBUG_COUT
    86         cout<<"list_len is "<<list_len<<" val_num is "<<val_num<<endl;
    87     #endif
    88     
    89     for(int i=0;i<list_len;i++)
    90     {
    91         for(int j=0;j<val_num;j++)
    92         {
    93             cout<<str_tmp[i][j]<<" ";
    94         }
    95             cout<<endl;
    96     }
    97 
    98     return 0;
    99 }
  • 相关阅读:
    【转载】怎样使用ZEMAX导出高质量的图像动画
    shell中的单引号,双引号,反引号
    docker容器以非root用户启动应用
    js操作json的基本方法
    页岩油
    shell中使用ssh
    强一致性 弱一致性 最终一致性
    CSV和excel
    workbook sheetname最大长度
    ipvs了解
  • 原文地址:https://www.cnblogs.com/linux-wang/p/9001418.html
Copyright © 2020-2023  润新知