I've looked at this and it wasn't much help.
我看过这个并没有多大帮助。
I have a Ruby program that puts a question to the cmd line and I would like to write a Python program that can return an answer. Does anyone know of any links or in general how I might go about doing this?
我有一个Ruby程序,它向cmd行提出一个问题,我想编写一个可以返回答案的Python程序。有谁知道任何链接或一般我怎么可能这样做?
Thanks for your help.
谢谢你的帮助。
EDIT
Thanks to the guys that mentioned piping. I haven't used it too much and was glad it was brought up since it forced me too look in to it more.
编辑感谢提到管道的人。我没有太多地使用它,并且很高兴它被提起,因为它迫使我更多地了解它。
5 个解决方案
#1
10
p = subprocess.Popen(['ruby', 'ruby_program.rb'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
ruby_question = p.stdout.readline()
answer = calculate_answer(ruby_question)
p.stdin.write(answer)
print p.communicate()[0] # prints further info ruby may show.
The last 2 lines could be made into one:
最后两行可以合二为一:
print p.communicate(answer)[0]
#2
4
If you're on unix / linux you can use piping:
如果你在unix / linux上,你可以使用管道:
question.rb | answer.py
Then the output of question.rb becomes the input of answer.py
然后question.rb的输出成为answer.py的输入
I've not tried it recently, but I have a feeling the same syntax might work on Windows as well.
我最近没有尝试过,但我觉得同样的语法也可以在Windows上运行。
#3
3
Pexpect
Pexpect的
http://www.noah.org/wiki/Pexpect
http://www.noah.org/wiki/Pexpect
Pexpect is a pure Python expect-like module. Pexpect makes Python a better tool for controlling other applications.
Pexpect是一个纯粹的Python期望模块。 Pexpect使Python成为控制其他应用程序的更好工具。
Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes' Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands.
Pexpect是一个用于生成子应用程序的纯Python模块;控制他们;并回应其产出中的预期模式。 Pexpect就像Don Libes的Expect一样。 Pexpect允许您的脚本生成子应用程序并控制它,就像人类正在键入命令一样。
#4
3
First of all check this out: [Unix piping][1]
首先检查一下:[Unix管道] [1]
It works on windows or unix but it's slighly dufferent, first the programs:
它可以在windows或unix上运行,但是它非常不同,首先是程序:
question.rb:
question.rb:
puts "This is the question"
answer.rb:
answer.rb:
question = gets
#calculate answer
puts "This is the answer"
Then the command line:
然后是命令行:
In unix:
在unix中:
question.rb | answer.rb
In windows:
在Windows中:
ruby question.rb | ruby answer.rb
Output:
输出:
This is the question
This is the answer
#5
1
There are two ways (off the top of my head) to do this. The simplest if you're in a Unix environment is to use piping. Simple example:
有两种方法(在我的头顶)做到这一点。如果您在Unix环境中最简单的方法是使用管道。简单的例子:
cat .profile .shrc | more
This will send the output of the first command (cat .profile .shrc
) to the more
command using the pipe character |
.
这将使用管道字符|将第一个命令(cat .profile .shrc)的输出发送到more命令。
The second way is to call one program from the other in your source code. I'm don't know how Ruby handles this, but in Python you can run a program and get it's output by using the popen function. See this example chapter from Learning Python, then Ctrl-F for "popen" for some example code.
第二种方法是在源代码中调用另一个程序。我不知道Ruby如何处理这个问题,但是在Python中你可以运行一个程序并使用popen函数得到它的输出。请参阅Learning Python中的示例章节,然后使用Ctrl-F获取“popen”以获取示例代码。
#1
10
p = subprocess.Popen(['ruby', 'ruby_program.rb'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
ruby_question = p.stdout.readline()
answer = calculate_answer(ruby_question)
p.stdin.write(answer)
print p.communicate()[0] # prints further info ruby may show.
The last 2 lines could be made into one:
最后两行可以合二为一:
print p.communicate(answer)[0]
#2
4
If you're on unix / linux you can use piping:
如果你在unix / linux上,你可以使用管道:
question.rb | answer.py
Then the output of question.rb becomes the input of answer.py
然后question.rb的输出成为answer.py的输入
I've not tried it recently, but I have a feeling the same syntax might work on Windows as well.
我最近没有尝试过,但我觉得同样的语法也可以在Windows上运行。
#3
3
Pexpect
Pexpect的
http://www.noah.org/wiki/Pexpect
http://www.noah.org/wiki/Pexpect
Pexpect is a pure Python expect-like module. Pexpect makes Python a better tool for controlling other applications.
Pexpect是一个纯粹的Python期望模块。 Pexpect使Python成为控制其他应用程序的更好工具。
Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes' Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands.
Pexpect是一个用于生成子应用程序的纯Python模块;控制他们;并回应其产出中的预期模式。 Pexpect就像Don Libes的Expect一样。 Pexpect允许您的脚本生成子应用程序并控制它,就像人类正在键入命令一样。
#4
3
First of all check this out: [Unix piping][1]
首先检查一下:[Unix管道] [1]
It works on windows or unix but it's slighly dufferent, first the programs:
它可以在windows或unix上运行,但是它非常不同,首先是程序:
question.rb:
question.rb:
puts "This is the question"
answer.rb:
answer.rb:
question = gets
#calculate answer
puts "This is the answer"
Then the command line:
然后是命令行:
In unix:
在unix中:
question.rb | answer.rb
In windows:
在Windows中:
ruby question.rb | ruby answer.rb
Output:
输出:
This is the question
This is the answer
#5
1
There are two ways (off the top of my head) to do this. The simplest if you're in a Unix environment is to use piping. Simple example:
有两种方法(在我的头顶)做到这一点。如果您在Unix环境中最简单的方法是使用管道。简单的例子:
cat .profile .shrc | more
This will send the output of the first command (cat .profile .shrc
) to the more
command using the pipe character |
.
这将使用管道字符|将第一个命令(cat .profile .shrc)的输出发送到more命令。
The second way is to call one program from the other in your source code. I'm don't know how Ruby handles this, but in Python you can run a program and get it's output by using the popen function. See this example chapter from Learning Python, then Ctrl-F for "popen" for some example code.
第二种方法是在源代码中调用另一个程序。我不知道Ruby如何处理这个问题,但是在Python中你可以运行一个程序并使用popen函数得到它的输出。请参阅Learning Python中的示例章节,然后使用Ctrl-F获取“popen”以获取示例代码。