运行 并获取 python C 程序的结果
Running and obtaining results of a python program in C
python 脚本包含大量导入的库
到目前为止我的 C 代码:
#include <stdio.h>
#include <python2.7/Python.h>
void main(int argc, char *argv[])
{
FILE* file;
Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
file = fopen("analyze.py","r");
PyRun_SimpleFile(file, "analyze.py");
Py_Finalize();
return;
}
有没有我可以使用的任何其他方法,以便即使我在 c 程序中调用的参数或 python 脚本数量的任何修改增加,也可以使用相同的代码,只需稍加改动?
我可以使用系统调用并使用从中获得的结果吗?
您可以这样从 C 程序调用 python 文件:
char command[50] = "python full_path_name\file_name.py";
system(command);
这段代码对我有用...
我没用过# include < python2.7/Python.h>
您可以将 python 文件中的结果写入任何文本文件,然后使用存储在文本文件中的结果做任何您想做的事情...
您还可以查看此 post 以获得进一步的帮助:
Calling python script from C++ and using its output
一种有用的方法是在 c 中调用 python 函数,这是您需要的而不是执行整个脚本。
如中所述
>> here
python 脚本包含大量导入的库
到目前为止我的 C 代码:
#include <stdio.h>
#include <python2.7/Python.h>
void main(int argc, char *argv[])
{
FILE* file;
Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
file = fopen("analyze.py","r");
PyRun_SimpleFile(file, "analyze.py");
Py_Finalize();
return;
}
有没有我可以使用的任何其他方法,以便即使我在 c 程序中调用的参数或 python 脚本数量的任何修改增加,也可以使用相同的代码,只需稍加改动?
我可以使用系统调用并使用从中获得的结果吗?
您可以这样从 C 程序调用 python 文件:
char command[50] = "python full_path_name\file_name.py";
system(command);
这段代码对我有用...
我没用过# include < python2.7/Python.h>
您可以将 python 文件中的结果写入任何文本文件,然后使用存储在文本文件中的结果做任何您想做的事情...
您还可以查看此 post 以获得进一步的帮助:
Calling python script from C++ and using its output
一种有用的方法是在 c 中调用 python 函数,这是您需要的而不是执行整个脚本。
如中所述 >> here