转自:https://blog.csdn.net/lwlgzy/article/details/83857297

http://www.cnblogs.com/jiaping/p/6321859.html

https://www.cnblogs.com/lvpengms/archive/2010/02/03/1663071.html

https://www.jb51.net/article/64094.htm

linux下安装qt请看:https://www.cnblogs.com/kimyeee/p/7250560.html

#include <stdio.h>

#include <Python.h>

int main(int argc, char* argv[])

{  

    PyObject *modulename, *module, *dic, *func, *args, *rel, *list;

    char *funcname1 = "sum";

    char *funcname2 = "strsplit";

    int i;

    Py_ssize_t s;

    printf("-==在C中嵌入Python==-\n");

    /* Python解释器的初始化*/

    Py_Initialize();                     

    if(!Py_IsInitialized())  

    {  

        printf("初始化失败!");  

        return -1;  

    }
PyRun_SimpleString("import sys");//导入模块
PyRun_SimpleString("sys.path.append('./')");//路径 /* 导入Python模块,并检验是否正确导入 */ modulename = Py_BuildValue("s", "pytest");//pytest.py文件放在可执行文件夹下 module = PyImport_Import(modulename); if(!module) { printf("导入pytest失败!"); return -1; } /* 获得模块中函数并检验其有效性 */ dic = PyModule_GetDict(module); if(!dic) { printf("错误!\n"); return -1; } /* 获得sum函数地址并验证 */ func = PyDict_GetItemString(dic,funcname1); if(!PyCallable_Check(func)) { printf("不能找到函数 %s",funcname1); return -1; } /* 构建列表 */ list = PyList_New(5); printf("使用Python中的sum函数求解下列数之和\n"); for (i = 0; i < 5; i++) { printf("%d\t",i); PyList_SetItem(list,i,Py_BuildValue("i",i)); } printf("\n"); /* 构建sum函数的参数元组*/ args = PyTuple_New(1); PyTuple_SetItem(args,0,list); /* 调用sum函数 */ PyObject_CallObject(func,args); /* 获得strsplit函数地址并验证*/ func = PyDict_GetItemString(dic,funcname2); if(!PyCallable_Check(func)) { printf("不能找到函数 %s",funcname2); return -1; } /* 构建strsplit函数的参数元组 */ args = PyTuple_New(2); printf("使用Python中的函数分割以下字符串:\n"); printf("this is an example\n"); PyTuple_SetItem(args,0,Py_BuildValue("s","this is an example")); PyTuple_SetItem(args,1,Py_BuildValue("s"," ")); /* 调用strsplit函数并获得返回值 */ rel = PyObject_CallObject(func, args); s = PyList_Size(rel); printf("结果如下所示:\n"); for ( i = 0; i < s; i ++) { printf("%s\n",PyString_AsString(PyList_GetItem(rel,i))); } /* 释放资源 */ Py_DECREF(list); Py_DECREF(args); Py_DECREF(module); /* 结束Python解释器 */ Py_Finalize(); printf("按回车键退出程序:\n"); getchar(); return 0; }

2.3 数据类型

Python定义了六种数据类型:整型、浮点型、字符串、元组、列表和字典,在使用C语言对Python进行功能扩展时,首先要了解如何在C和Python的数据类型间进行转化。

2.3.1 整型、浮点型和字符串

在Python的C语言扩展中要用到整型、浮点型和字符串这三种数据类型时相对比较简单,只需要知道如何生成和维护它们就可以了。下面的例子给出了如何在C语言中使用Python的这三种数据类型:

例2:typeifs.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// build an integer
PyObject* pInt = Py_BuildValue("i", 2003);
assert(PyInt_Check(pInt));
int i = PyInt_AsLong(pInt);
Py_DECREF(pInt);
// build a float
PyObject* pFloat = Py_BuildValue("f", 3.14f);
assert(PyFloat_Check(pFloat));
float f = PyFloat_AsDouble(pFloat);
Py_DECREF(pFloat);
// build a string
PyObject* pString = Py_BuildValue("s", "Python");
assert(PyString_Check(pString);
int nLen = PyString_Size(pString);
char* s = PyString_AsString(pString);
Py_DECREF(pString);
2.3.2 元组

Python语言中的元组是一个长度固定的数组,当Python解释器调用C语言扩展中的方法时,所有非关键字(non-keyword)参数都以元组方式进行传递。下面的例子示范了如何在C语言中使用Python的元组类型:

例3:typetuple.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// create the tuple
PyObject* pTuple = PyTuple_New(3);
assert(PyTuple_Check(pTuple));
assert(PyTuple_Size(pTuple) == 3);
// set the item
PyTuple_SetItem(pTuple, 0, Py_BuildValue("i", 2003));
PyTuple_SetItem(pTuple, 1, Py_BuildValue("f", 3.14f));
PyTuple_SetItem(pTuple, 2, Py_BuildValue("s", "Python"));
// parse tuple items
int i;
float f;
char *s;
if (!PyArg_ParseTuple(pTuple, "ifs", &i, &f, &s))
  PyErr_SetString(PyExc_TypeError, "invalid parameter");
// cleanup
Py_DECREF(pTuple);
2.3.3 列表

Python语言中的列表是一个长度可变的数组,列表比元组更为灵活,使用列表可以对其存储的Python对象进行随机访问。下面的例子示范了如何在C语言中使用Python的列表类型:

例4:typelist.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// create the list
PyObject* pList = PyList_New(3); // new reference
assert(PyList_Check(pList));
// set some initial values
for(int i = 0; i < 3; ++i)
  PyList_SetItem(pList, i, Py_BuildValue("i", i));
// insert an item
PyList_Insert(pList, 2, Py_BuildValue("s", "inserted"));
// append an item
PyList_Append(pList, Py_BuildValue("s", "appended"));
// sort the list
PyList_Sort(pList);
// reverse the list
PyList_Reverse(pList);
// fetch and manipulate a list slice
PyObject* pSlice = PyList_GetSlice(pList, 2, 4); // new reference
for(int j = 0; j < PyList_Size(pSlice); ++j) {
 PyObject *pValue = PyList_GetItem(pList, j);
 assert(pValue);
}
Py_DECREF(pSlice);
// cleanup
Py_DECREF(pList);
2.3.4 字典

Python语言中的字典是一个根据关键字进行访问的数据类型。下面的例子示范了如何在C语言中使用Python的字典类型:

例5:typedic.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// create the dictionary
PyObject* pDict = PyDict_New(); // new reference
assert(PyDict_Check(pDict));
// add a few named values
PyDict_SetItemString(pDict, "first",
           Py_BuildValue("i", 2003));
PyDict_SetItemString(pDict, "second",
           Py_BuildValue("f", 3.14f));
// enumerate all named values
PyObject* pKeys = PyDict_Keys(); // new reference
for(int i = 0; i < PyList_Size(pKeys); ++i) {
 PyObject *pKey = PyList_GetItem(pKeys, i);
 PyObject *pValue = PyDict_GetItem(pDict, pKey);
 assert(pValue);
}
Py_DECREF(pKeys);
// remove a named value
PyDict_DelItemString(pDict, "second");
// cleanup
Py_DECREF(pDict);

7、数据转换,在c/c++与python交互时,都是通过PyObject来传 入和传出数据的,Python提供相关函数对PyObject数据进行转换,转换时使用格式字符串来控制生成的对象类型,具体可参见 https://docs.python.org/3.5/c-api/arg.html官方文档:

  A) 将c/c++数据转换成PyObject:

    PyObject *pInt=Py_BuildValue("i",2003);

    PyObject *pStr=Py_BuildValue("s","This is a string");

    PyObject *pTuples=Py_BuildValue("()"); //生成空元组,可作为调用不包含任何参数的函数时,传递空参数

    PyObject *pTuples=Py_BuildValue("(s)","This is a string");  //生成一个元素的元组,可作为调用只包含一个字符参数的函数时,传递一个字符参数

  B) 将PyObject数据转换成c/c++数据:

    1) int bb=0;  PyArg_Parse(pObjcet,"i",&bb);   //这里pObject是包含整数数据的Python对象,第二个字符串参数"i"指定转换类型,第三个参数将结果值存入bb变量;

    2) char * cc=NULL;  PyArg_parse(pObject,"s",&cc);   //这是字符串转换

    3) char * cc=NULL;  PyArg_parse(pObject,"(s)",&cc);   //这是包含一个字符串元素元组转换

8、调用Python模块函数时,传入参数时,要构造一个参数元组,如:presult = PyObject_CallObject(pfunction, args);这里args就是一个元组,作为被调用函数的参数列表;

  A、如参数为空,则这样构造:args=Py_BuildValues("(si)","abc",10);  表示构造二个参数的元组,一个是字符型,另一个是整;多个参数,可参照处;

  B、如果参数为空,则需构造一个包含0个元素元组:args=Py_BuildValues("()");

  注意以上二种都在格式字符串中包含"()",这是指示构造元;作为函数调用参数必须传递元组,也必须这样构;

  下例是通过可变参数来构造调用函数参数元组:

int PythonHandler::PyModuleRunFunction(const char *module, const char *function,
        const char *result_format, void *result, const char *args_format, ...)
{
......
    //这里构造调用函数所使用的参数元组
    va_list args_list;
va_start(args_list, args_format);
args = Py_VaBuildValue(const_cast<char *>(args_format), args_list);
va_end(args_list);
  ...
  if (!args)
{
        //args为空,则元组构造失败
Py_DECREF(pfunction);
return -3;
}
...
presult = PyObject_CallObject(pfunction,args); //调用函数 PS:我只把暂时用得到的拿过来了,还有很多有用的可以查看转自的网站,想了解更多查看python官网
https://docs.python.org/2/c-api/object.html

中标麒麟(linux)下Qt调用python的更多相关文章

  1. 中标麒麟(linux)下Qt调用python数据转换

    转自:https://blog.csdn.net/itas109/article/details/78733478 mytest.py文件 # -*- coding: utf-8 -*- def he ...

  2. Linux下Qt调用共享库文件.so

    修改已有的pro文件,添加如下几句: INCLUDEPATH += /home/ubuntu/camera/camera/LIBS += -L/home/ubuntu/camera/camera -l ...

  3. 基于Linux(中标麒麟)上QT的环境搭建

    最近由于公司需要,需要在中标麒麟上进行QT的二次开发,但是网上的资料很少,就算是有也基本都是其他的版本的Linux上的搭建.中标麒麟本身的资料也很好,而且还只能试用60天. 下面就介绍下我对此环境的搭 ...

  4. 国产中标麒麟Linux部署dotnet core 环境并运行项目 (二) 部署运行控制台项目

    背景 在上一篇文章安装dotnet core,已经安装好dotnet core了.之前只是安装成功了dotnet, 输入dotnet --info,可以确认安装成功了,但是在运行代码时,还是报错了,本 ...

  5. Linux下Qt的安装与配置

    参考资料:http://www.cnblogs.com/emouse/archive/2013/01/28/2880142.html Linux 下编译.安装.配置 QT 下载qt 这里用的是4.7. ...

  6. linux下java调用.so动态库方法2: JNA

    摘自:http://blog.csdn.net/todorovchen/article/details/21319033 另请参见: http://blog.sina.com.cn/s/blog_8c ...

  7. linux下java调用.so文件的方法1: JNI

    摘自http://blog.163.com/squall_smile/blog/static/6034984020129296931793/ https://my.oschina.net/simabe ...

  8. [转]linux下编译boost.python

    转自:http://blog.csdn.net/gong_xucheng/article/details/25045407 linux下编译boost.python 最近项目使用c++操作python ...

  9. linux下C调用lua的第一个程序

    linux下C调用lua的第一个程序 linux的环境是Fedora 18,运行在VM workstation中,以开发模式安装,自带了lua 5.1.4,可以在命令行上直接用lua命令进入到lua环 ...

随机推荐

  1. winCVS 使用的一个小要点

    对于版本管理软件CVS,可以在Linux中使用命令来管理. 但是 在windows 界面下,也可以使用 winCVS 工具来管理. 现在 讲一下 如何 在 winCVS 登陆 CVS 帐号 和 密码: ...

  2. 权限管理demo-Http请求前后监听工具

    工具作用: 1. 输出每次请求的参数 2. 接口的请求时间 package com.mmall.common; import com.mmall.util.JsonMapper; import lom ...

  3. 十六进制颜色值和rgb颜色值互相转换

    在之前的一篇文章<将16进制的颜色转为rgb颜色>中,曾经写过将16进制的颜色转换为rgb颜色. 当然了,今天再看,还是有很多可以优化的地方,所以对之前的代码重构了一遍,并且同时写了一个反 ...

  4. 【机器学习_7】numpy

    1. 定义 入门选手不抄作业了,先简单理解下,numpy最大的应用大概是方便做数学运算啥的,功劳在于一个叫ndarray(多维数组)的数据类型.numpy本身没有强大分析能力,只是pandas的基础啥 ...

  5. Vue Loader

    介绍 允许为 Vue 组件的每个部分使用其它的 webpack loader,例如在 <style> 的部分使用 Sass 和在 <template> 的部分使用 Pug(模板 ...

  6. leetcode62

    使用排列组合计算公式来计算,注意使用long long型数据保证计算不会溢出. class Solution { public: int M, N; ; //从根到叶子有多少个分支,就表示有多少种路径 ...

  7. leetcode240

    public class Solution { public bool SearchMatrix(int[,] matrix, int target) { , j = matrix.GetLength ...

  8. [C语言]进阶|链表

    --------------------------------------------------------------------------------------- 可变数组: array. ...

  9. AOP方法增强自身内部方法调用无效 SpringCache 例子

    开启注解@EnableCaChing,配置CacheManager,结合注解@Cacheable,@CacheEvit,@CachePut对数据进行缓存操作 缺点:内部调用,非Public方法上使用注 ...

  10. 一直觉得用java很不顺心

    一直觉得用java很不顺心,今儿想明白一个事情.如果把汇编比作石器时代,c作为冷兵器时代,c++作为工业革命之后的热兵器及机械化时代,而c#之类则进入了现代科学世界,至于go,python之流,大概可 ...