我正在使用org。蟒蛇util。PythonInterpreter类在java中执行python代码。请在下面找到我的代码片段。
PythonInterpreter pythonInterpreter = new PythonInterpreter(null, new PySystemState());
ByteArrayOutputStream outStream = new ByteArrayOutputStream(16384);
pythonInterpreter.setOut(outStream);
pythonInterpreter.setErr(outStream);
// execute the code
pythonInterpreter.exec(script);
String consoleOutput = outStream.toString();
outStream.flush();
System.out.println("Console output :- "+consoleOutput);
相同的
脚本有时我得到“consoleOutput”为空。我想不出这个问题。对于运行上述代码1000次,我至少得到4次空输出。
PythonInterpreter pythonInterpreter = new PythonInterpreter();