Java中执行python函数的方式有多种,如果需要得到返回值,这里推荐一种方式,可以使用jython嵌入到代码里运行。
依赖和代码都在下面了,需要自取,都是多次调试后的最终版,绝对能跑。
<!-- https://mvnrepository.com/artifact/org.python/jython-standalone -->
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
import java.util.*;
/**
* @author lilong
* @date 2020/12/01
* @desc
*/
@Slf4j
public class PythonUtil {
/**
* 设置一个关键key,用于从python中获取结果
*/
private static String resultKey = "result";
/**
* 系统变量
*/
static {
Properties props = new Properties();
props.put("python.home", "path to the Lib folder");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preProps = System.getProperties();
PythonInterpreter.initialize(preProps, props, new String[0]);
}
/**
* 执行python脚本
* @param express 脚本,需要注意换行格式正确
* @param map 入参变量集合,key为变量名,value为变量值
* @return
*/
public static String doExpress(String express, Map<String, Object> map) {
try (PythonInterpreter pythonInterpreter = new PythonInterpreter()) {
map.forEach((key, value) -> {
PyObject pyValue;
if (value instanceof String) {
//中文乱码解决
pyValue = Py.newStringUTF8(value.toString());
} else if (value instanceof Float || value instanceof Double) {
//python和java浮点精度不一致问题,都转为double
pyValue = Py.newFloat(Double.parseDouble(value.toString()));
} else if (value instanceof Integer) {
pyValue = Py.newInteger((int) value);
} else {
pyValue = Py.java2py(value);
}
pythonInterpreter.set(key, pyValue);
});
express = express.replace("return", resultKey + "=");
log.info("input map:{},express:{}", map, express);
PyString pyString = Py.newStringUTF8(express);
pythonInterpreter.exec(pyString);
return pythonInterpreter.get(resultKey).toString();
} catch (Exception e) {
log.error("执行python脚本出错: {} input: \n{} \nexpress: \n{}", e.toString(), map, express);
throw new RuntimeException("执行python脚本出错" + e.toString());
}
}
public static void main(String[] args) {
Map<String, Object> input = new HashMap<>(4);
String express =
"if args in [1.0,'1','挑葱夫']:\n" +
" return 1\n" +
"else:\n" +
" return 0";
input.put("args", "1");
log.info(doExpress(express, input));
input.put("args", "挑葱夫");
log.info(doExpress(express, input));
input.put("args", 1.0);
log.info(doExpress(express, input));
}
}
有问题留言沟通,谢谢。