我需要打开一个 R 脚本并为其提供由单独的 python 脚本制定的输入。 subprocess
模块似乎是执行此操作的好方法。
虽然我遇到了一些令人费解的结果,即我显然可以通过 p.stdin
写一次且只能写一次。这是我到目前为止所拥有的:
from subprocess import Popen, PIPE, STDOUT
p = Popen(['r --no-save'],stdin=PIPE,stdout=PIPE,stderr=PIPE,shell=True)
p.stdin.write("source('myrscript.R')\n")
p.stdin.write('myfirstinput')
当我运行这段代码时发生的事情是 stdin.write()
的第一个实例按预期执行(并打开我的 R 脚本),但第二行什么都不做,子进程 (实际上,R 脚本)以错误退出,表明子处理在预期输入的地方没有收到输入,因此终止。
注意- 在一个完美的世界中,我会直接通过 R 进行交互,但这个特定的脚本需要复杂的输入,不能直接用于实际目的。此外,rpy/rpy2 不是一个选项,因为此脚本的最终用户不一定有权访问该模块或其依赖项。 rscript
也不是一个选项(出于多种原因,但主要是因为最终用户 R 配置的可变性)。
最后,p.communicate
不是一个选项,因为显然这会在写入后关闭进程,我需要保持它打开。
提前致谢
最佳答案
你需要调用.communicate()
:
from subprocess import Popen, PIPE, STDOUT
p = Popen(
['r', '--nosave'],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE)
p.stdin.write("source('myrscript.R')\n")
p.stdin.write('myfirstinput\n')
p.stdin.write('q\n')
stdout, stderr = p.communicate()
print '---STDOUT---'
print stdout
print '---STDERR---'
print stderr
print '---'
讨论
- 我不使用
shell=True
,它似乎可以使用我的假 R 脚本,因为我的系统中没有安装 R。您可能需要也可能不需要。 - 我更喜欢将命令行分解成如图所示的字符串列表,但单个字符串如
r --nosave
也可以;只是不要同时做。 - 不要忘记
stdin.write()
不会写入换行符\n
,您必须自己提供。
更新
我的第一次尝试没有成功,我希望第二次尝试更接近。正如 J.F. Sebastian 所建议的那样,您可能希望使用 pexpect
:
import pexpect
import sys
if __name__ == '__main__':
prompt = '> ' # Don't know what the R prompt looks like
lines = ['one', 'two', 'three']
r = pexpect.spawn('r --no-save', logfile=sys.stdout)
for line in lines:
r.expect(prompt)
r.sendline(line)
# If you want to interact with your script, use these two lines
# Otherwise, comment them out
r.logfile = None # Turn off logging to sys.stdout
r.interact()
讨论
- 您可能需要安装
pexpect
。我用pip install pexpect
做到了
- 如果您不想与系统交互,请注释掉最后两行,但一定要发送一些信号让 R 脚本退出。
spawn()
返回一个spawn
对象,参见文档 here .
关于Python 子进程 - 写入多个标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15790599/