从C ++调用Python的脚本,并使用其输出 [英] Calling python script from C++ and using its output

查看:357
本文介绍了从C ++调用Python的脚本,并使用其输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C ++调用一个python脚本,并希望使用这个脚本生成的输出.csv文件回C ++。我在尝试这样做的main():

I want to call a python script from C++ and wish to use the output .csv file generated by this script back into C++. I tried this in main():

std::string filename = "/home/abc/xyz/script.py";
std::string command = "python ";
command += filename;
system(command.c_str());

这不会调用和执行Python脚本。

This does call and execute the python script.

EDIT_2 打印在正在执行的Python命令......被印在屏幕上的东西时,脚本调用。到现在为止还挺好。但!它不创建.csv文件(在同一个脚本的一部分)。这样的:我有一个 training.csv 100项文件。我叫python脚本,用的脚本小变化,使trainig.csv文件现在应该只包含50个条目,而不是100。其覆盖即可。但是,没有这样的事情发生。在脚本(打印,等等)的命令其余的都是完美的工作。
training.csv 文件是用C来读取++通常使用的 fstream的函数getline ...

EDIT_2 The 'print' commands in the python are being executed...things are being printed on the screen when the script is called. So far so good. But! it is not creating the .csv file (part of the same script). like: i had a training.csv file with 100 entries. I called the python script, with little changes to the script so that the trainig.csv file now should contain only 50 entries instead of 100. Its overwritten. But, no such thing happening. Rest of the commands in script (print, etc) are working perfectly. The training.csv file is to be read with C++ normally using fstream and getline...

任何想法,该怎么办呢?

Any idea how to do it?

修改使用Linux

谢谢!

推荐答案

下面是从你的C ++应用程序中嵌入你的Python模块执行的解决方案。它并不比分叉/通过系统调用执行你的Python脚本更好或最坏的,它只是用不同的方式来做到这一点。无论是最好还是不依赖于上下文和使用。

Here is a solution to embed the execution of your python module from within your C++ application. It's not better or worst than forking/executing your python script through a system call, it just is a different way to do it. Whether it is best or not depend on your context and usage.

前段时间,我有codeD的方式来加载Python模块作为插件来一个C ++应用程序,这里的<一个href=\"https://github.com/hackable-devices/polluxnzcity/blob/master/PolluxGateway/src/pollux/pollux_extension.C\">interesting部分。

Some time ago I have coded a way to load python modules as plugins to a C++ application, here's the interesting part.

基本上,你需要的#include&LT; Python.h&GT; ,那么 Py_Initialize()来启动蟒蛇间preTER。

Basically, you need to #include <Python.h>, then Py_Initialize() to start your python interpreter.

然后你就进口SYS ,使用 PyRun_SimpleString(进口SYS); ,你可以加载你的插件通过执行 PyRun_SimpleString('sys.path.append(路径/要/我的/模块/)')

Then you do import sys, using : PyRun_SimpleString("import sys");, and you can load your plugin by doing PyRun_SimpleString('sys.path.append("path/to/my/module/")').

要℃之间++和Python的交换价值,事情变得更难,你必须转换所有的C ++对象到Python对象(开始在我的脚本行69)。

To exchange values between C++ and Python, things get harder, you have to to transform all your C++ objects into python objects (starting line 69 in my script).

然后,您可以使用调用你的函数 PyObject_Call_Object(...),利用你作为参数创建的Python对象。

Then you can call your function using PyObject_Call_Object(...), using all the python objects you created as arguments.

您获得的返回值,并转换成在C ++对象的所有这些值。而且不要忘了内存管理,所有的!

You get the return value, and transforms all those values in C++ objects. And don't forget the memory management in all that!

要结束你的Python间preTER,一个简单的调用 Py_Finalize()

To end your python interpreter, a simple call to Py_Finalize().

这真的看起来难度比它确实是,但你必须要非常小心这样做,因为这可能会导致泄漏,安全问题等。

It really looks harder than it is really, but you have to be really careful doing this, because it could lead to leaks, security issues etc..

这篇关于从C ++调用Python的脚本,并使用其输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆