gpt4 book ai didi

java - 在 python 中使用它们将 2 个参数从 Java 传递到 Python

转载 作者:行者123 更新时间:2023-12-01 16:59:03 24 4
gpt4 key购买 nike

我正在将字符串值从 java 传递到 python 脚本。在我的 python 脚本中,我正在使用参数,但我无法读取 2 个值。请帮助

Java 代码:

String filePath = "E:\\Project_ivin\\test.py";  
ProcessBuilder pb = new ProcessBuilder().command("python", "-u", filePath, ""+issueId+""+comments);
Process p = pb.start();

Python 脚本:

sys.argv[1] # issueid

sys.argv[2] # comments

最佳答案

似乎缺少逗号,否则使用第三个参数

Java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
public static void main(String[] args) throws IOException, InterruptedException {
String[] list = { "python", "-u", "in.py", "issueId", "comments" };
ProcessBuilder pb = new ProcessBuilder().command(list);
Process p = pb.start();
System.out.println("" + pb.command());
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null)
sb.append(line);
System.out.println(sb.toString());
}
}

Python:

# print(sys.argv[0]) // this will be file name

print(sys.argv[1])
print(sys.argv[2])

关于java - 在 python 中使用它们将 2 个参数从 Java 传递到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61541578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com