![](/img/trans.png)
[英]Call java program (with arguments) from Python and return results back to Python
[英]Pass multiple list of parameters from python to java program and get back return code using subprocess.communicate
需要以下代碼的幫助-我想將三個列表的字符串元素作為輸入傳遞給我在python腳本中調用的java程序。 到目前為止,這是我所做的-
import subprocess
from subprocess import PIPE
from subprocess import Popen
amount = ['23.5', '12.5', '56.0', '176.1']
invoice_no = ['11290', '12892', '12802', '23489']
date = ['2/3/19', '12/30/17', '04/21/2018', '8/12/17', '12/21/18']
## problem is proc.communicate(). I am currently passing only invoice_no[i] as input.
for i in range (0, len(invoice_no)):
proc = subprocess.Popen(['java', '-jar', '/data/python/xyz.jar'],stdin=PIPE,stdout=PIPE, stderr=STDOUT)
q = proc.communicate(input = invoice_no[i])
print(q)
## But I want to pass amount and date as input as well.
## I dont know the syntax for that. I also want output as 1 or 0 i.e success or failure. Does anyone know syntax for this?
我看到的大多數問題都是將單個字符串作為輸入傳遞的,但這不是我想要的。 甚至子過程的正式文檔也對找出如何傳遞多個輸入語法沒有幫助。 鏈接- 從python腳本運行Java程序
由於我已經找到答案。 我以為我會在這里張貼。 可能有人會發現它有用-
import subprocess
from subprocess import PIPE
from subprocess import Popen
amount = ['23.5', '12.5', '56.0', '176.1']
invoice_no = ['11290', '12892', '12802', '23489']
date = ['2/3/19', '12/30/17', '04/21/2018', '8/12/17', '12/21/18']
for i in range (0, len(invoice_no)):
proc = subprocess.Popen(['java', '-jar', '/data/python/xyz.jar', invoice_no[i], date[i], amount[i]],stdin=PIPE,stdout=PIPE, stderr=PIPE)
output = proc.communicate()[0] ## this will capture the output of script called in the parent script.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.