Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将java参数传递给python,从java调用python代码_Java_Python_Parameter Passing_Processbuilder - Fatal编程技术网

如何将java参数传递给python,从java调用python代码

如何将java参数传递给python,从java调用python代码,java,python,parameter-passing,processbuilder,Java,Python,Parameter Passing,Processbuilder,我创建了简单的python语句 我的_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/py

我创建了简单的python语句 我的_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
。但它不适用于浮动:

>>> "123".isdigit()
True
>>> "12.3".isdigit()
False

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
。但它不适用于浮动:

>>> "123".isdigit()
True
>>> "12.3".isdigit()
False
在爪哇

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
在爪哇

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字符串类不提供
isdigit
?为什么尝试强制转换该值?
isdigit()
如果只需要整数,也可以。但是对于
float
,它返回
False
。python字符串类不提供
isdigit
?为什么尝试强制转换该值?
isdigit()
如果只需要整数,也可以。但是对于
float
,它返回
False