【问题标题】:How to send jar file output to a json file using python如何使用python将jar文件输出发送到json文件
【发布时间】:2019-08-13 07:51:03
【问题描述】:

我有 jar 文件,执行时会在终端上打印输出,我不确定如何将 jar 文件输出作为 json 文件传递​​。

下面的代码只是在终端打印jar文件输出

subprocess.Popen(['java', '-jar', '/home/myfolder/collect.jar'])

我正在考虑下面但不知道从...开始......

with open('collect.json', 'w') as fp:
    xxxxxxxxxxx

希望有人可以提供进一步的建议。谢谢。

【问题讨论】:

标签: python json file jar


【解决方案1】:

你可以试试这样的

with open('collect.json','w') as fp :
  subprocess.Popen('java -jar .//home/myfolder/collect.jar',stdout=fp).wait()

有关更多信息,请参阅此问题How to get the output from .jar execution in python codes?

我希望这会有所帮助。

【讨论】:

  • hi....this getting error raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
  • 我添加 [] 如下 subprocess.Popen(['java -jar .//home/myfolder/collect.jar'],stdout=fp).wait()
【解决方案2】:

这应该可以解决问题。如果您查看subprocess documentation,您可以看到 check_output 运行带有参数的命令并将其输出作为字节字符串返回。

import multiprocessing as mp
import subprocess

command = "java -jar /home/myfolder/collect.jar"

def runCommand(q):
    commandOutput = subprocess.check_output(command.split()).decode("utf-8")
    q.put(commandOutput)

q = mp.Queue()
commandProcess = mp.Process(target=runCommand, args=(q, ))
commandProcess.start()
output = q.get()
print(output)
with open('collect.json', w) as fp: 
    fp.write(output)

没有运行代码,但应该可以工作。

【讨论】:

  • 嗨...我收到错误 ImportError: No module named mutiprocessing.my pip list does have mutiprocess install
  • 对不起...多处理的拼写错误...我再次运行代码...无论如何...代码运行但 json 文件为空...没有数据...跨度>
  • @chenoi 再试一下上面的代码,我要实际写文件(见 fp.write(output))
  • 它有效...非常感谢您...我之前发现了缺失的部分...感谢您的关注和支持...
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多