我想连接一个c方法与python脚本。我做到了用:连接C方法与python脚本
#include<stdio.h>
void pythonEx(){
system("pythonScript.py");
}
这样我可以运行Python脚本,但我不知道如何从方法到python脚本传递数据。
我想连接一个c方法与python脚本。我做到了用:连接C方法与python脚本
#include<stdio.h>
void pythonEx(){
system("pythonScript.py");
}
这样我可以运行Python脚本,但我不知道如何从方法到python脚本传递数据。
我建议在这里通过JNI去。
首先,你需要JNI代码,将调用Python的lib:
#include "jni.h"
#include "runscript27.h"
#include "python/Python.h"
#include <stdio.h>
static PyThreadState *tstate = NULL;
static PyGILState_STATE gstate = 0;
// Initialization should be done once
JNIEXPORT void JNICALL Java_python_Python_pyinitialize
(JNIEnv *env, jclass obj) {
Py_Initialize();
// This is important!!
// Remember to reserve PyGILState
// It is not thread safe
gstate = PyGILState_Ensure();
}
// Finalization should be done once
JNIEXPORT void JNICALL Java_python_Python_pyfinalize
(JNIEnv *env, jclass obj) {
// once finished remember to release GIL
PyGILState_Release(gstate);
Py_Finalize();
}
JNIEXPORT void JNICALL Java_python_Python_runstring27
(JNIEnv *env, jclass obj, jstring str) {
// we have to get string bytes into C string
const char *c_str;
c_str = (*env)->GetStringUTFChars(env, str, NULL);
if(c_str == NULL) {
return;
}
// Some info for the user
printf("Script from python.Python: %s\n", c_str);
PyRun_SimpleString(c_str);
// after using it, remember to release the memory
(*env)->ReleaseStringUTFChars(env, str, c_str);
}
JNIEXPORT void JNICALL Java_python_Python_runscript27
(JNIEnv *env, jclass obj, jstring str, jstring name) {
// we have to get string bytes into C string
const char *c_str;
c_str = (*env)->GetStringUTFChars(env, str, NULL);
if(c_str == NULL) {
return;
}
// we have to get string bytes into C string
const char *c_name;
c_name = (*env)->GetStringUTFChars(env, name, NULL);
if(c_name == NULL) {
return;
}
// Some info for the user
printf("Script from python.Python: %s\n", c_name);
printf("File with script: %s\n",c_str);
// We will open file passed as argument
// and call this script via Python library
FILE* file;
file = fopen(c_str,"r");
PyRun_SimpleFile(file, c_str);
// close file, clean up, and return. That's it.
fclose(file);
// after using it, remember to release the memory
(*env)->ReleaseStringUTFChars(env, str, c_str);
(*env)->ReleaseStringUTFChars(env, str, c_name);
}
然后,您可以从Java调用此代码:
package python;
import java.io.*;
public class Python {
static {
System.loadLibrary("callpy27");
Python.pyinitialize();
}
public static native void runscript27(String script, String name);
public static native void runstring27(String script);
public static native void pyinitialize();
public static native void pyfinalize();
public static void main(String [] arg) {
// Super simple sctipt to call
String script = "print 'Hello from python'";
// run script from file
try {
PrintWriter writer = new PrintWriter("script.py", "UTF-8");
writer.println(script);
writer.flush();
writer.close();
pyinitialize();
runscript27("script.py", "test");
pyfinalize();
} catch(Exception ex) {
ex.printStackTrace();
}
// run script passed as string
try {
pyinitialize();
runstring27(script);
pyfinalize();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
对于全样本(带的Makefile和示例代码,看看这里:https://github.com/mkopsnc/keplerhacks/tree/master/python)
通过数据如何?使用命令行? –
难道你不能把数据作为参数传递给python脚本吗? –
本链接详细介绍了在C程序中使用python的更好方法: http://www.linuxjournal.com/article/8497 –