Java では、stdout を文字列として読み取ることができます。
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdout));
String toUse = stdout.toString();
/**
* do all my fancy stuff with string `toUse` here
*/
//Now that I am done, set it back to the console
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
誰かがPythonでこれを行う同等の方法を教えてもらえますか? Python: Closing a for loop by reading stdoutやHow to get stdout into a string (Python)など、この質問のさまざまなフレーバーが何度も尋ねられていることを私は知っています。しかし、必要なものはそれよりも単純なので、必要なものを取得するためにサブプロセスをインポートする必要はないと感じています。私はEclipseでpydevを使用していますが、プログラムは非常に簡単です。
私はすでに試しました
from sys import stdout
def findHello():
print "hello world"
myString = stdout
y = 9 if "ell" in myString else 13
しかし、それは機能していないようです。ファイルを開くことについていくつかの苦情があります。