转自: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. layui流加载+h5自带模板

    @{ ViewBag.Title = "服务列表"; Layout = "~/Areas/hahaha/Views/Shared/_Head.cshtml"; ...

  2. PHP chdir函数:改变当前的目录

    PHP chdir函数的作用是改变当前的目录,这里主机吧详细介绍下chdir函数的用法,并列举使用chdir函数的例子. chdir定义和用法: chdir() 函数改变当前的目录. chdir实例: ...

  3. vue-cli 选项无法选问题

    winpty vue.cmd create admin 这样创建就可以了

  4. python3笔记<一>基础语法

    随着AI人工智能的兴起,网络安全的普及,不论是网络安全工程师还是AI人工智能工程师,都选择了Python.(所以本菜也来开始上手Python) Python作为当下流行的脚本语言,其能力不言而喻,跨平 ...

  5. gdb 使用

    2018年7月27日21:05:16 —— 多进程调试 1.follow_fork_mode 作用:在fork之后跟随父进程还是子进程 可以使用 show follow_fork_mode查看再for ...

  6. Echarts报错 Can't read property 'getWidth' of null

    统计图报错: 这里的报错与echarts无关,与zrender有关,zrender是echarts依赖的canvas绘图库 你不需要了解zrender,这个问题是你代码出了错 谨记::代码的错

  7. 非virtual函数,用指针进行upcast

    void print_func(A* p) { p -> print(); } int main() { A a(); B b(,); //a.print(); //b.print(); pri ...

  8. 用python优雅打开文件及上下文管理协议

    有次面试被问到如何优雅地打开一个文件?   那就是用with语句,调用过后可以自动关闭.   但是为什么使用with语句就可以自动关闭呢,原因就是上下文管理协议.   上下文管理协议:包含方法 __e ...

  9. Linux下MySQL5.7.18 yum方式从卸载到安装

    本文出处:http://www.cnblogs.com/wy123/p/6932166.html 折腾了大半天,看了想,想了看,总算是弄清楚yum安装的过程了,之前写过二进制包安装的,这里用yum安装 ...

  10. Ubuntu 装nexus

    装nexus前提是装好JDK和maven 先下载 wget http://download.sonatype.com/nexus/oss/nexus-2.12.0-01-bundle.tar.gz 再 ...