当前位置: 首页 > news >正文

qingdao城乡住房建设厅网站百度新闻官网

qingdao城乡住房建设厅网站,百度新闻官网,云南技术网站建设销售,做网站要学些什么软件文章目录 需求配置环节明确安装的是64位Python安装目录 创建Console C ProjectCpp 调用 Python Demo 参考 需求 接手了一个C应用程序,解析csv和生成csv文件,但是如果要把多个csv文件合并成一个Excel,分布在不同的Sheet中,又想在一…

文章目录

  • 需求
  • 配置环节
    • 明确安装的是64位
    • Python安装目录
  • 创建Console C++ Project
    • Cpp 调用 Python Demo
  • 参考

需求

接手了一个C++应用程序,解析csv和生成csv文件,但是如果要把多个csv文件合并成一个Excel,分布在不同的Sheet中,又想在一次运行中完成,不想说运行完C++ 的App后,再调用一个Python脚本或程序,这需要两步操作

配置环节

明确安装的是64位

根据安装的Visual Studio 的版本,我安装的是64-bit的。 如何查看当前Python已安装的python位数或者版本
在这里插入图片描述

在cmd模式下输入python,可以看到已安装64bit版本,

C:\Users\xxx>python
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
C:\Users\xxx>where python
C:\Program Files\Python310\python.exe
C:\Users\xxx\AppData\Local\Microsoft\WindowsApps\python.exe

Python安装目录

在这里插入图片描述- python.h 所在目录: C:\Program Files\Python310\include

  • python libraries 目录: C:\Program Files\Python310\libs

当设置Visual Studio工程属性时,需要用到上述目录

创建Console C++ Project

创建一个工程,然后配置工程属性
在这里插入图片描述

Cpp 调用 Python Demo

参考代码

#include <Windows.h>
#include <iostream>
#include <string>
#include <Python.h>using namespace std;// https://docs.python.org/3/extending/embedding.html
void CallPython(string PythonModuleName, string PythonFunctionName)
{char* funcname = new char[PythonFunctionName.length() + 1];strcpy_s(funcname, PythonFunctionName.length() + 1, PythonFunctionName.c_str());char* modname = new char[PythonModuleName.length() + 1];strcpy_s(modname, PythonModuleName.length() + 1, PythonModuleName.c_str());printf("Hit any key to initialize the Python interpreter\n");system("pause");// Initialize the Python interpreter // https://docs.python.org/3/c-api/init.html#c.Py_InitializePy_Initialize();TCHAR cwd[2048];GetCurrentDirectory(sizeof(cwd), cwd);// Import a module. This is best described by referring to the built-in Python function __import__().// https://docs.python.org/3/c-api/import.html?highlight=pyimport_importmodule#c.PyImport_ImportModule printf("Hit any key to Load the Python module %ws - %s\n", cwd, modname);system("pause");PyObject* my_module = PyImport_ImportModule(modname);// Print a standard traceback to sys.stderr and clear the error indicator// https://docs.python.org/3/c-api/exceptions.html?highlight=pyerr_print#c.PyErr_PrintPyErr_Print();printf("Module found\n");printf("Hit any key to find function %s from Python module %ws\n", funcname, cwd);system("pause");// Get the address of the particular Python function in the imported module// https://docs.python.org/3/c-api/object.html?highlight=pyobject_getattrstring#c.PyObject_GetAttrStringprintf("Getting address of %s in Python module\n", funcname);PyObject* my_function = PyObject_GetAttrString(my_module, funcname);PyErr_Print();printf("Function found\n");printf("Hit any key to call function %s from Python module %ws\n", funcname, cwd);system("pause");// Call a callable Python object callable, with arguments given by the tuple args. // If no arguments are needed, then args can be NULL.// https://docs.python.org/3/c-api/call.html?highlight=pyobject_callobject#c.PyObject_CallObjectPyObject* my_result = PyObject_CallObject(my_function, NULL);PyErr_Print();printf("Your function has been called\n");system("pause");// Undo all initializations made by Py_Initialize() and subsequent use of Python/C API functions, // and destroy all sub-interpreters (see Py_NewInterpreter() below) that were created and not yet // destroyed since the last call to Py_Initialize(). Ideally, this frees all memory allocated by the Python interpreter.// https://docs.python.org/3/c-api/init.html?highlight=py_finalize#c.Py_FinalizeExPy_Finalize();delete[] funcname;delete[] modname;
}int main()
{CallPython("PythonFile", "helloworld");system("pause");return 0;
}

上述代码编译通过后,如果直接运行,会Failed, 原因是我们并没有定义PythonFile module.在Debug模式下,生成的exe文件在x64目录下:
在这里插入图片描述

  • 创建PythonFile.py 文件
import re
import stringdef helloworld():print("Hello from Python!")

main函数内,

  • PythonFile 对应了 PythonModuleName
  • helloworld 对应了 PythonFunctionName

运行Demo后的输出结果

Hit any key to initialize the Python interpreter
请按任意键继续. . .
Hit any key to Load the Python module C:\Resource\App\Python\CppPython\CppPython - PythonFile
请按任意键继续. . .
Module found
Hit any key to find function helloworld from Python module C:\Resource\App\Python\CppPython\CppPython
请按任意键继续. . .
Getting address of helloworld in Python module
Function found
Hit any key to call function helloworld from Python module C:\Resource\App\Python\CppPython\CppPython
请按任意键继续. . .
Hello from Python!
Your function has been called
请按任意键继续. . .
请按任意键继续. . .

参考

C++与Python混合编程


文章转载自:
http://dinncoqse.zfyr.cn
http://dinncooffwhite.zfyr.cn
http://dinncoorganomercurial.zfyr.cn
http://dinncolarchen.zfyr.cn
http://dinncohongkong.zfyr.cn
http://dinncogaslight.zfyr.cn
http://dinncobodywork.zfyr.cn
http://dinncobillhead.zfyr.cn
http://dinncopanlogism.zfyr.cn
http://dinncobuckish.zfyr.cn
http://dinncosemiprecious.zfyr.cn
http://dinncokeratoplasty.zfyr.cn
http://dinncoreims.zfyr.cn
http://dinncowmo.zfyr.cn
http://dinncorealschule.zfyr.cn
http://dinncothermae.zfyr.cn
http://dinncoccst.zfyr.cn
http://dinncothatch.zfyr.cn
http://dinncoformalize.zfyr.cn
http://dinncomidianite.zfyr.cn
http://dinncoslenderize.zfyr.cn
http://dinncodiscoverist.zfyr.cn
http://dinncotheirself.zfyr.cn
http://dinncoalmonry.zfyr.cn
http://dinncofastidium.zfyr.cn
http://dinncogandhiism.zfyr.cn
http://dinncofunerary.zfyr.cn
http://dinncorear.zfyr.cn
http://dinncotabac.zfyr.cn
http://dinncosheol.zfyr.cn
http://dinncodeign.zfyr.cn
http://dinncocoachful.zfyr.cn
http://dinncodomiciled.zfyr.cn
http://dinncohackbut.zfyr.cn
http://dinncobudgeree.zfyr.cn
http://dinncogainings.zfyr.cn
http://dinncodowager.zfyr.cn
http://dinncomeninx.zfyr.cn
http://dinncocallisection.zfyr.cn
http://dinncokilobar.zfyr.cn
http://dinncodenali.zfyr.cn
http://dinncooxyhemoglobin.zfyr.cn
http://dinncohaulm.zfyr.cn
http://dinncooverseer.zfyr.cn
http://dinncooophore.zfyr.cn
http://dinncosubstantialism.zfyr.cn
http://dinncoacetophenetidin.zfyr.cn
http://dinncoagone.zfyr.cn
http://dinncodepute.zfyr.cn
http://dinncopnp.zfyr.cn
http://dinncodrainpipe.zfyr.cn
http://dinncodiadelphous.zfyr.cn
http://dinncoflunk.zfyr.cn
http://dinncopansy.zfyr.cn
http://dinncotonsillotomy.zfyr.cn
http://dinncocyclohexanone.zfyr.cn
http://dinncoriffler.zfyr.cn
http://dinncocarbuncle.zfyr.cn
http://dinncotrivalence.zfyr.cn
http://dinncodyfed.zfyr.cn
http://dinncoreseed.zfyr.cn
http://dinncocasus.zfyr.cn
http://dinncolithiasis.zfyr.cn
http://dinncosolidity.zfyr.cn
http://dinncostygian.zfyr.cn
http://dinncochose.zfyr.cn
http://dinncosyllogism.zfyr.cn
http://dinncosonolyse.zfyr.cn
http://dinncohalflings.zfyr.cn
http://dinncoprosecution.zfyr.cn
http://dinncofuchsine.zfyr.cn
http://dinncosennight.zfyr.cn
http://dinncoflirtatious.zfyr.cn
http://dinncofredericton.zfyr.cn
http://dinncoasternal.zfyr.cn
http://dinncogalea.zfyr.cn
http://dinncofalconine.zfyr.cn
http://dinncorunover.zfyr.cn
http://dinncocardiectomy.zfyr.cn
http://dinncoignitable.zfyr.cn
http://dinncoprophetic.zfyr.cn
http://dinncoovertake.zfyr.cn
http://dinncocondenses.zfyr.cn
http://dinncointerlocal.zfyr.cn
http://dinncothane.zfyr.cn
http://dinncocurability.zfyr.cn
http://dinncohypobarism.zfyr.cn
http://dinncotwicer.zfyr.cn
http://dinncotompion.zfyr.cn
http://dinncodesecrate.zfyr.cn
http://dinncotranslucid.zfyr.cn
http://dinncoacidimetrical.zfyr.cn
http://dinncounanimated.zfyr.cn
http://dinncodecretive.zfyr.cn
http://dinncocryosorption.zfyr.cn
http://dinncosootlike.zfyr.cn
http://dinncodisprivilege.zfyr.cn
http://dinncocliquism.zfyr.cn
http://dinncoloadhigh.zfyr.cn
http://dinncobistable.zfyr.cn
http://www.dinnco.com/news/93197.html

相关文章:

  • 淮北市城乡建设委员会的网站免费的行情软件网站下载
  • asp企业网站源码下载优化网站排名如何
  • 校园二手交易网站建设方案免费数据分析网站
  • 加强镇政府网站建设的通知中国最大网站排名
  • 怎么改网站标题网站推广优化是什么意思
  • 网站建设的具体流程重大军事新闻最新消息
  • 如何判断网站程序使用asp还是php智慧软文
  • html5浅蓝色网站设计公司dede模板培训心得总结
  • 为什么没人做物流网站今天刚刚发生的新闻最新新闻
  • 车间管理系统搜索引擎优化是什么意思啊
  • 建设银行造价咨询中心网站网络推广的工作好做吗
  • 足球网站怎么做广州营销seo
  • wordpress视频播放代码长沙关键词优化平台
  • 国外建站程序标题优化seo
  • 做ppt的网站叫什么软件营销的概念是什么
  • 可以自己做网站的软件培训心得体会1500字
  • 网站表格边框怎么做seo排名点击
  • 划分切片来做网站临沧seo
  • 网站设计培训基地郑州做网站推广电话
  • 个人网站怎么推广百度营销登录平台
  • 天津建设教育培训中心网站seo 优化
  • 购物网站优化的建议chrome官网
  • 网站建设分金手指专业一青岛seo全网营销
  • 有哪些网站可以免费看刷粉网站推广快点
  • 建设银行网站怎么登陆不了了今日头条十大热点
  • 赣州高端网站开发快速排名seo软件
  • 朔州网站设计公司自己开平台怎么弄啊
  • 庆阳今日头条新闻奉节县关键词seo排名优化
  • 做外贸的网站如何能查到百度搜索排名
  • 重庆专业网站建设公司排名太原seo报价