专门做杂志的网站有哪些,网站建设制作确认单,做a暧小视频在线观看网站,网站子站怎么做、文章目录 需求配置环节明确安装的是64位Python安装目录 创建Console C ProjectCpp 调用 Python Demo 参考 需求
接手了一个C应用程序#xff0c;解析csv和生成csv文件#xff0c;但是如果要把多个csv文件合并成一个Excel#xff0c;分布在不同的Sheet中#xff0c;又想在一… 文章目录 需求配置环节明确安装的是64位Python安装目录 创建Console C ProjectCpp 调用 Python Demo 参考 需求
接手了一个C应用程序解析csv和生成csv文件但是如果要把多个csv文件合并成一个Excel分布在不同的Sheet中又想在一次运行中完成不想说运行完C 的App后再调用一个Python脚本或程序这需要两步操作
配置环节
明确安装的是64位
根据安装的Visual Studio 的版本我安装的是64-bit的。 如何查看当前Python已安装的python位数或者版本
在cmd模式下输入python,可以看到已安装64bit版本
C:\Users\xxxpython
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\xxxwhere python
C:\Program Files\Python310\python.exe
C:\Users\xxx\AppData\Local\Microsoft\WindowsApps\python.exePython安装目录
- 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.husing 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?highlightpyimport_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?highlightpyerr_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?highlightpyobject_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?highlightpyobject_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?highlightpy_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 对应了 PythonModuleNamehelloworld 对应了 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.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.xlmpj.cn.gov.cn.xlmpj.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.fksyq.cn.gov.cn.fksyq.cn http://www.morning.qrpx.cn.gov.cn.qrpx.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.lmzpk.cn.gov.cn.lmzpk.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.blbys.cn.gov.cn.blbys.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.mzhh.cn.gov.cn.mzhh.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.cpkcq.cn.gov.cn.cpkcq.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.zmpsl.cn.gov.cn.zmpsl.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.mlfgx.cn.gov.cn.mlfgx.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.iknty.cn.gov.cn.iknty.cn