擅长:python、mysql、java
<pre><code>import sys
from io import StringIO
class capt_stdout:
def __init__(self):
self._stdout = None
self._string_io = None
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._string_io = StringIO()
return self
def __exit__(self, type, value, traceback):
sys.stdout = self._stdout
@property
def string(self):
return self._string_io.getvalue()
</code></pre>
<p>这样使用:</p>
^{pr2}$