gpt4 book ai didi

python - 从 R 调用 python 并即时输出到控制台

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:45 24 4
gpt4 key购买 nike

我使用 R 命令从 R 运行 python 脚本:

system('python test.py')

但是在 python 程序完成之前,我在 test.py 中的打印语句不会出现在 R 控制台中。当 python 程序在 R 中运行时,我想查看打印语句。我也尝试过 sys.stdout.write(),但结果是一样的。非常感谢任何帮助。

这是我的 test.py 代码:

import time

for i in range(10):
print 'i=',i
time.sleep(5)

最佳答案

使用 R v3.0.1 在 Windows 8 上测试

只需右键单击 r 控制台,然后取消勾选/取消选择 Buffered Output 选项(见下图)。现在执行您的代码,您将看到 print 语句的输出!

image-file

更新:

我忘了提到我还需要在 python 文件中的 print 语句之后添加 sys.stdout.flush()

import time
import sys

for i in range(5):
print 'i=',i
sys.stdout.flush()
time.sleep(1)

此外,如果您选择 Buffered Output 选项,那么当您在脚本执行期间左键单击 r 控制台时,您将看到输出。继续单击并显示输出。 :)

关于python - 从 R 调用 python 并即时输出到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23814700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com