如何将 java 参数传递给 python ,从 Java 调用 Python 代码
How to pass the java parameter to python , Call Python code from Java
我创建了简单的 python 语句
my_utils.py
def adder(a, b):
c=a+b
return c
我想给 Java 中的 python 赋值。
public class ParameterPy {
public static void main(String a[]){
try{
int number1 = 100;
int number2 = 200;
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://my_utils.py",""+number1,""+number2);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null){
System.out.println("Python Output: " + line);
}
System.out.println("........end process.......");
}catch(Exception e){System.out.println(e);}
}
}
但是process builder无法将参数值传递给python中的a,b并显示结果。
如何给python参数赋值?如果数值有效?如果我将非数字值(例如字符串)传递给 python
怎么样?
def str(myWord):
if myWord=="OK":
print "the word is OK."
else:
print " the word is not OK."
Python sys
模块通过 sys.argv
提供对任何命令行参数的访问。但是参数的类型总是字符串。这是我想如何检查数值的示例:
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
def adder(a, b):
c=a+b
return c
def is_number(x):
try:
float(x)
return True
except ValueError:
return False
p1 = sys.argv[1]
p2 = sys.argv[2]
if is_number(p1) and is_number(p2):
print "Sum: {}".format(adder(float(p1), float(p2)))
else:
print "Concatenation: {}".format(adder(p1, p2))
更新:正如@cricket_007 提到的,如果您只想检查整数,可以使用isdigit
。但它不适用于 float
:
>>> "123".isdigit()
True
>>> "12.3".isdigit()
False
import sys
# print sys.argv
print "sys.argv is:",sys.argv
# ['D://my_utils.py', '100', '200', 'google']
a= sys.argv[1]
b= sys.argv[2]
print "a is:", a
print "b is:", b
a= int (a)
b= int(b)
def adder(a, b):
c=a+b
return c
print adder(a,b)
searchTerm=sys.argv[3]
print searchTerm ##google
def word(searchTerm):
if searchTerm=="google":
print " you get it"
else:
print " the word is different."
word(searchTerm)
在 java
int number1 = 100;
int number2 = 200;
String searchTerm="google";
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://searchTestJava//my_utils.py",""+number1,""+number2,""+searchTerm);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null){
System.out.println("Python Output: " + line);
输出结果:
Python Output: a is: 100
Python Output: b is: 200
Python Output: 300
Python Output: google
Python Output: you get it
我创建了简单的 python 语句 my_utils.py
def adder(a, b):
c=a+b
return c
我想给 Java 中的 python 赋值。
public class ParameterPy {
public static void main(String a[]){
try{
int number1 = 100;
int number2 = 200;
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://my_utils.py",""+number1,""+number2);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null){
System.out.println("Python Output: " + line);
}
System.out.println("........end process.......");
}catch(Exception e){System.out.println(e);}
}
}
但是process builder无法将参数值传递给python中的a,b并显示结果。
如何给python参数赋值?如果数值有效?如果我将非数字值(例如字符串)传递给 python
怎么样?def str(myWord):
if myWord=="OK":
print "the word is OK."
else:
print " the word is not OK."
Python sys
模块通过 sys.argv
提供对任何命令行参数的访问。但是参数的类型总是字符串。这是我想如何检查数值的示例:
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
def adder(a, b):
c=a+b
return c
def is_number(x):
try:
float(x)
return True
except ValueError:
return False
p1 = sys.argv[1]
p2 = sys.argv[2]
if is_number(p1) and is_number(p2):
print "Sum: {}".format(adder(float(p1), float(p2)))
else:
print "Concatenation: {}".format(adder(p1, p2))
更新:正如@cricket_007 提到的,如果您只想检查整数,可以使用isdigit
。但它不适用于 float
:
>>> "123".isdigit()
True
>>> "12.3".isdigit()
False
import sys
# print sys.argv
print "sys.argv is:",sys.argv
# ['D://my_utils.py', '100', '200', 'google']
a= sys.argv[1]
b= sys.argv[2]
print "a is:", a
print "b is:", b
a= int (a)
b= int(b)
def adder(a, b):
c=a+b
return c
print adder(a,b)
searchTerm=sys.argv[3]
print searchTerm ##google
def word(searchTerm):
if searchTerm=="google":
print " you get it"
else:
print " the word is different."
word(searchTerm)
在 java
int number1 = 100;
int number2 = 200;
String searchTerm="google";
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://searchTestJava//my_utils.py",""+number1,""+number2,""+searchTerm);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null){
System.out.println("Python Output: " + line);
输出结果:
Python Output: a is: 100
Python Output: b is: 200
Python Output: 300
Python Output: google
Python Output: you get it