c++调用python解析list返回值

来源:互联网 发布:replay是什么牌子知乎 编辑:程序博客网 时间:2024/06/16 11:37
按照常例:直接上代码
moduleInterface.cpp
#include "common.h"#include "moduleInterface.h"#ifndef _USRDLLint main(){wstring moduleName = L"checkPID";wstring methodName = L"checkPid";wstring prameter = L"fdafdasfda 610121820313221 ghhhhhhhdss# +_)_+(32131gfddsfgs@#()!123 a510101800110202adfa 610121198203132212fdaf 610121198203132210dfsafda 123213610121198203132212123214";mapString dict;pair<mapString::iterator, bool> insert_pair;dict.insert(pair<wstring, wstring>(L"content",L"you known"));insert_pair  = dict.insert(mapString::value_type(L"context", L"Mizrahi"));//int ret = CallModuleMethod(moduleName, methodName, prameter, dict);if(insert_pair.second == true){cout<<"insert success!\n";list<string> ret = CallModuleMethod(moduleName, methodName, prameter, dict);if(ret.empty()){cout<<"call CallModuleMethod failed!\n";return -1;}else{cout<<"call CallModuleMethod success!\n";return 0;}}else{cout<<"insert failed!\n";return -1;}}#endif/*!\author: zxh\date 2013/10/10\version ver1.0\param moduleName -the param is a full path of module\param methodName -function of module\param prameter - param of function\param dict - the param is a value of already exists \exception\test\note\attention\sa\remark 1.2013/10/10 15 : 05 created by zxh version  \return*/SP_MODULE_DLLEXPORT list<string> CallModuleMethod(wstring moduleName, wstring methodName, wstring prameter, mapString &dict){list<string> result;if((methodName.empty()) || (moduleName.empty()) || (prameter.empty())){cout<<"function CallModuleMethod is null!\n";return result;}Py_Initialize();if(!Py_IsInitialized()){cout<<"Py_initialize failed!\n";return result;}PyObject *pModule,*pFunc;//载入modulepModule = PyImport_ImportModule(XMLch2char(moduleName.c_str()).c_str());if(!pModule){PyErr_Print();cout<<"can't find "<<XMLch2char(moduleName.c_str()).c_str()<<".py file\n";Py_Finalize();return result;}//load the function pFunc = PyObject_GetAttrString(pModule, XMLch2char(methodName.c_str()).c_str());if(!pFunc || !PyCallable_Check(pFunc)){PyErr_Print();cout<<"can't find function "<<"["<<XMLch2char(methodName.c_str()).c_str()<<"]"<<endl;Py_DECREF(pModule);Py_Finalize();return result;}PyObject *pArgs = PyTuple_New(2);PyObject *pDict = PyDict_New();//创建字典类型变量if(!dict.empty()){mapString::iterator iter = dict.begin();while(iter != dict.end()){PyDict_SetItemString(pDict,XMLch2char(iter->first.c_str()).c_str(), Py_BuildValue("s", XMLch2char(iter->second.c_str()).c_str()));//往字典类型变量中添加数据++iter;}}PyTuple_SetItem(pArgs, 0, Py_BuildValue("s", XMLch2char(prameter.c_str()).c_str()));PyTuple_SetItem(pArgs, 1, pDict);//1---序号  将字典类型变量添加到参数元组中 PyObject *pVal = PyObject_CallObject(pFunc, pArgs);int size = PyList_Size(pVal);cout<<"List size: "<<size<<endl;mapString::iterator iter = dict.begin();while(iter != dict.end()){cout<<XMLch2char(iter->first.c_str()).c_str()<<":"<<\XMLch2char(iter->second.c_str()).c_str()<<endl;iter++;}int ret;//标志位//int times;//符合条件的数据总数char* cstr;//符合条件的数据PyObject *item;char tmp[MAX] = {0};for(int i = 0; i< size; ++i){PyObject *pRet = PyList_GetItem(pVal, i);if(PyList_Check(pRet)){int len = PyList_Size(pRet);for(int j = 0; j < len; ++j){item = PyList_GetItem(pRet,j);PyArg_Parse(item, "s", &cstr);result.push_back(string(cstr));cout<<cstr<<endl;}continue;}PyArg_Parse(pRet, "i", &ret);sprintf(tmp,"%d\n",ret);result.push_back(string(tmp));cout<<ret<<endl;}//PyObject *pRet = PyList_GetItem(pVal, 0);//PyArg_Parse(pRet, "i", &ret);//cout<<ret<<endl;//PyObject *pRet1 = PyList_GetItem(pVal, 1);//PyArg_Parse(pRet1, "i", ×);//cout<<times<<endl;//PyObject *pRet2 = PyList_GetItem(pVal, 2);//int len = PyList_Size(pRet2);//cout<<"len="<<len<<endl;//for(int j = 0;j < len; ++j)//{//item = PyList_GetItem(pRet2,j);//PyArg_Parse(item, "s", &cstr);////const char* temp = DetectBufCode(cstr,strlen(cstr));//cout<<cstr<<endl;////cout<<conv_anycode_utf8(cstr, "utf-8",temp)<<endl;//}Py_DECREF(pArgs);Py_DECREF(pModule);Py_Finalize();return result;}   
moduleInterface.h
#include <iostream>#include <string>#include <map>#include <list>#include <iostream>#include "common.h"#include "Python.h"using namespace std;#pragma comment (lib,"python25.lib")#define MAX 1024#ifdef _USRDLL#define SP_MODULE_DLLEXPORT __declspec(dllexport)#else#define SP_MODULE_DLLEXPORT #endiftypedef map<wstring, wstring> mapString;//struct Result//{//mapString dict;//mapString content;//int sum;//};list<string> SP_MODULE_DLLEXPORT CallModuleMethod(wstring moduleName, wstring methodName, wstring prameter, mapString &dict);
运行结果如下:
原创粉丝点击