[英]Getting Python to return more than one value to Java
我使用了来自此链接的信息,在下面编写了以下代码来运行Python脚本,并使其返回单个字符串:
public static void Option_2_Output()
{
Scanner Option2_Input = new Scanner(System.in);
System.out.println("");
System.out.println("===============================================");
System.out.println(" Testing Means of Two Groups ");
System.out.println("===============================================");
System.out.println("");
String Option2_program = "python";
String Option2_path = "C:\\PathtoScript\\Python_Test.py";
try{
String Option2_datapath;
System.out.print("Please type the location of your Data : ");
Option2_datapath = Option2_Input.next();
ProcessBuilder Option2_process = new ProcessBuilder(Option2_program,Option2_path,"" + Option2_datapath);
Process Option2_Output = Option2_process.start();
BufferedReader in = new BufferedReader(new InputStreamReader(Option2_Output.getInputStream()));
String Option2_Final = new String(in.readLine());
System.out.println(Option2_Final);
}catch(Exception e){
System.out.println(e);
}
}
对象Option2_Final
将Python脚本的最后打印输出作为字符串流回。 如何获得不止一个价值?
由于仅利用了一个readLine()
函数,因此仅流回一行。 可以编写一个for循环来搜索每条额外的行,或者可以为要传递回的每行创建对象:
BufferedReader in = new BufferedReader(new InputStreamReader(Option2_Output.getInputStream()));
String Option2_Final_1 = new String(in.readLine()); //Line 1
String Option2_Final_2 = new String(in.readLine()); //Line 2
System.out.println(Option2_Final_1); //Print Line 1
System.out.println(Option2_Final); //Print Line 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.