【问题标题】:Pass multiple list of parameters from python to java program and get back return code using subprocess.communicate将多个参数列表从 python 传递到 java 程序并使用 subprocess.communicate 取回返回码
【发布时间】:2019-06-15 22:19:11
【问题描述】:

需要以下代码的帮助 - 我想将三个列表的字符串元素作为输入传递给我在 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? 

我看到的大多数问题都是将单个字符串作为输入传递。但这不是我想要的。即使是 subprocess 的官方文档也无助于找出如何传递多个输入语法。链接是-running java program from python script

How to execute java program using python considering inputs and outputs both

【问题讨论】:

  • 不应该 -jar 参数也应该是 Popen 的单独参数吗?
  • 不完全确定.. 我是 python 新手。在浏览了许多问题和文件后,我能够达到这一点。

标签: java python python-2.7 subprocess


【解决方案1】:

因为我找到了答案。我想我会把它贴在这里。可能有人会觉得它有用 -

   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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    相关资源
    最近更新 更多