C++調用python(C++)

C++源代碼:python部分就是正常的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 }
相關文章
相關標籤/搜索