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

商城网站做推广方案免费行情网站

商城网站做推广方案,免费行情网站,网页软件有哪些,wordpress 文章 两边文章目录 需求配置环节明确安装的是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://www.khdw.cn/news/22178.html

相关文章:

  • 新手建站教程报价单免费网络推广渠道
  • 直接用ip做网站如何提高网站seo排名
  • 哪里做公司网站必应搜索引擎入口
  • 网站页面设计模板cba赛程
  • 企业融资的10种方法济南网站优化排名
  • 哪里可以做网站开发天津网站制作系统
  • 素材网站的下载服务器怎么做百度小程序排名优化
  • 代码网站开发深圳做网站的公司
  • 网站开发的技术支撑 经验能力金昌网站seo
  • 道滘镇网站建设公司谷歌广告平台
  • 青岛企业建设网站企业企业网站推广方案策划
  • 企业做网站怎么做全自动引流推广软件免费
  • discuz下载seo咨询顾问
  • 网站建设中可能出现的问题如何用html制作网页
  • 动态网站用什么语言做企业公司网站建设
  • 成都网站建设专家市场营销网站
  • 购买网站空间ftp设计百度指数查询入口
  • 郑州市网站制作公司外贸建站平台
  • 两颗米网站建设百度热议
  • 宣传式网站软文广告例子
  • 电脑系统优化软件哪个好用hyein seo是什么牌子
  • 搜讯网站建设企业做个网站多少钱
  • 医院网站建设模板下载快速的网站设计制作
  • 企业信息管理系统设计百度seo搜索营销新视角
  • 万网域名注册后如何做网站教学手机网站制作教程
  • 做外贸建网站需要多少钱seo排名优化价格
  • 商城手机网站制作新网站快速排名软件
  • 淘客推广网站怎么做的百度无锡营销中心
  • 百度seo营销网站外包公司排名
  • 老网站做成适合手机端的网站怎么做长沙seo报价