转自: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. 基于CentOS搭建个人Leanote云笔记本

    Leanote 依赖 MongoDB 作为数据存储,下面开始安装MongoDB: 1. 下载启动 MongoDB 下载 MongoDB 进入 /home 目录,并下载 MongoDB: cd /hom ...

  2. nowcoder 寻找(LCA)

    这个题貌似是过的最少的? smeow一眼给出了一个单log的算法orz 首先求出x和y的lca, x和c的lca,y和c的lca, 然后分类讨论以下就行了 实际上只有三种情况 #include< ...

  3. js 常用代码片段

    一.预加载图像 如果你的网页中需要使用大量初始不可见的(例如,悬停的)图像,那么可以预加载这些图像. function preloadImages(){ for(var i=0;i<argume ...

  4. LeetCode contest-95[876,877,👁878]

    876. Middle of the Linked List first submission # Definition for singly-linked list. # class ListNod ...

  5. CentOS7(64)环境使用rpm命令安装gcc

    第一步:下载gcc相关的安装文件下载地址:http://vault.centos.org/7.0.1406/os/x86_64/Packages/ 下载以下文件: cpp-4.8.2-16.el7.x ...

  6. JUC详解

    一.Java多线程 -- JUC包源码分析1 -- CAS/乐观锁 乐观锁其实就是不加锁,用CAS + 循环重试,实现多个线程/多个客户端,并发修改数据的问题 使用AtomicStampedRefer ...

  7. 使用 C++ REST SDK 进行网络编程

    安装 C++ REST SDK $ brew install cpprestsdk $ brew install boost $ brew install libressl 创建工程 打开 Xcode ...

  8. 使用PandoraBox时的软件源配置

    src/gz 18.12_core http://downloads.pangubox.com:6380/pandorabox/18.12/targets/ralink/mt7621/packages ...

  9. Android Studio 3.1.3正式版的新坑。。。

    Gradle编译时没问题,运行App时候出现: java.util.NoSuchElementException java.lang.RuntimeException: com.android.bui ...

  10. Java&Android TimeUtil ~ A Good Util!

    时间工具类 import android.text.TextUtils; import android.util.Log; import java.security.MessageDigest; im ...