【问题标题】:Simulate typing input into Java Program using Python使用 Python 模拟在 Java 程序中输入输入
【发布时间】:2016-12-14 19:03:08
【问题描述】:

我正在编写一个 Python 脚本来对大约 300 个使用 Scanner(System.in) 输入的作业进行评分

我认为我可以做的事情就像打电话一样简单:

    os.system("cat input.txt | java {} > program_output.txt".format(class_file))

我也试过

    os.system("java {} < input.txt > program_output.txt".format(class_file))

但是,在某些作业中,即使在手动输入输入时它工作得很好,但当它从这个 input.txt 文件中获取输入时,程序却给了我一个错误:

Exception in thread "main" java.util.NoSuchElementException: No line found

这是在之前两次调用 console.next() 之后,错误发生在调用 console.nextLine() 时

我不知道为什么会这样,但是修改每个人的代码并不是很简单,所以我想知道是否有一种方法可以使用 python 来模拟输入的实际类型,而不是重定向 stdin。谢谢!

编辑:为了记录,Java 程序不能接受来自标准输入的管道的原因是因为程序创建了多个扫描仪,而不是绕过扫描仪。

【问题讨论】:

  • 不要放弃重定向标准输入。这绝对是最好的方法。
  • 看看Jython,是python的java实现,可以原生导入java类。
  • 您是否尝试过使用子进程执行相同的操作?
  • @themistoklik 我尝试在 shell 中逐字输入该命令,但它给出了我的错误。
  • @JohnKugelman 你如何建议我改变我正在做的事情?

标签: java python linux unix stdin


【解决方案1】:

模拟实际人工输入的最佳方法是pexpect python module,它基于expect Linux 实用程序,而它又精确地用于处理交互式工具。

(一旦到达我的桌面,我将尝试提供示例)

【讨论】:

    【解决方案2】:

    您确定您的 input.txt 文件包含足够的换行符吗?即手动输入时所需的“输入”数量。

    要在 python 中执行此操作,请尝试使用 subprocess

    import subprocess
    
    with open('input.txt') as f:
    l=f.readlines()
    
    l=map(lambda x:x.strip(), l) #no newlines we don't want
    
    proc = subprocess.Popen(['python script.py'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    
    s = proc.communicate(input='\n'.join(l))
    

    其中 script.py 是一个类似于您的 java 的简单脚本,需要逐行输入

    script.py

    print "input:"
    raw_input()
    print 1
    raw_input()
    print 2
    print "done!"
    

    这模拟了发送列表中的每个元素以及在 shell 中的输入。打印 s,我们返回一个元组 (stdout,stderr) ,所以 s[0] 将返回你的 java 结果。

    请注意,您必须提供不少于 java 程序预期的行,如果可行,重定向标准输入也应该可以。

    【讨论】:

    • &lt; input.txt 有什么问题,为什么要解决这个问题?在我看来这会有同样的问题。
    • @JohnKugelman 我认为它没有问题,但是问题的标题要求模拟输入的 python 解决方案。我个人更喜欢重定向标准输入。我现在怀疑他的 input.txt 没有足够的\n 字符。 Python 也会抱怨 EOFError。
    • 我意识到的问题是 Java 程序有多个扫描器,当我从输入管道传输时存在竞争条件。
    • 我写了一个 AppleScript 脚本来在我的 python 程序需要我时输入命令,我只需要按播放 100 次。
    【解决方案3】:

    为了记录,我的解决方案是制作一个 AppleScript 程序,然后让我的程序调用 AppleScript 程序:

    activate application "Terminal"
    
    tell application "Terminal"
    
    activate
    
    
    delay 0.2
    
    tell application "System Events"
    
    keystroke "C"
    
    delay 0.2
    
    keystroke return
    
    keystroke "madlib.txt"
    
    delay 0.2
    
    keystroke return
    
    keystroke "madlib_output.txt"
    
    delay 0.2
    
    keystroke return
    
    keystroke "jeopardy"
    
    delay 0.2
    
    keystroke return
    
    keystroke "200"
    
    delay 0.2
    
    keystroke return
    
    keystroke "dollars"
    
    delay 0.2
    
    keystroke return
    
    keystroke "tallest"
    
    delay 0.2
    
    keystroke return
    
    keystroke "moutain"
    
    delay 0.2
    
    keystroke return
    
    keystroke "The United States of America"
    
    delay 0.2
    
    keystroke return
    
    keystroke "V"
    
    delay 0.2
    
    keystroke return
    
    keystroke "madlib_output.txt"
    
    delay 0.2
    
    keystroke return
    
    keystroke "Q"
    
    delay 0.2
    
    keystroke return
    
    end tell
    
    end tell
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2012-12-06
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 2015-08-04
      相关资源
      最近更新 更多