Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] 7 Easy Steps to Redirect Your Standard Output to a Variable (Python)
1
7 Easy Steps to Redirect Your Standard Output to a Variable (Python)

5/5 – (1 vote)

[Image: 1f4ac.png] Question: How to redirect the standard output in Python and store it as a string in a variable?

This article will guide you through seven easy steps to solve this problem. As an overview, here’s the code in eight lines that stores the standard output in a variable
Code:
my_result
:

  1. Code:
    from io import StringIO

  2. Code:
    import sys

  3. Code:
    tmp = sys.stdout

  4. Code:
    my_result = StringIO()

  5. Code:
    sys.stdout = my_result

  6. Code:
    print('hello world') # output stored in my_result

  7. Code:
    sys.stdout = tmp

  8. Code:
    print(result.getvalue())

Let’s go over those steps one by one—we’ll examine the full code for copy&paste at the end of this article, so read on! [Image: 1f440.png]

Step 1: Import libraries StringIO and sys

Import the two libraries
Code:
StringIO
and
Code:
sys
to access the standard output and store the string input-output stream.

Code:
from io import StringIO import sys

Both modules are part of the standard library, so there is no need to install them with pip!

Step 2: Keep stdout in temporary variable

We’ll overwrite the standard output to catch everything written to it. In order to reset your code to the normal state, we need to capture the original standard output stream by introducing a temporary variable.

Code:
tmp = sys.stdout
Step 3: Capture standard output using a StringIO object

Create a variable and assign a
Code:
StringIO
object to the variable to capture the standard output stream.

Code:
my_result = StringIO()

Now, this object can store everything printed to the standard output. But we have to connect it first to the stdout!

Step 4: Assign Standard Output Stream to StringIO object

Assign the
Code:
StringIO
object created in the previous step to the standard output that is captured with
Code:
sys.stdout
.

Code:
sys.stdout = my_result
Step 5: Print to the standard output

From this point onwards, anything that is printed using the
Code:
print()
statement by any function you call in your Python script is written in the
Code:
StringIO
object referred to by variable
Code:
my_result
.

The following exemplifies the
Code:
print('hello world')
statement but you can do anything here:

Code:
print('hello world')

[Image: 1f4a1.png] Note: No output appears on the screen anymore because the standard output is now redirected to the variable.

Step 6: Clean up by redirecting stdout to Python shell

Are you ready with capturing the output in the variable? Clean up by redirecting the standard output stream from the variable to the screen again.

Code:
sys.stdout = tmp
Step 7: Get and print the string from stdout

At this point, your string from the standard output is stored in the
Code:
StringIO
object in the
Code:
my_result
variable. You can access it using the
Code:
StringIO.getvalue()
method.

Code:
print(result.getvalue())
Full Code

Here’s the full code snippet for ease of copy&paste:

Code:
# Step 1 from io import StringIO import sys # Step 2 tmp = sys.stdout # Step 3 my_result = StringIO() # Step 4 sys.stdout = my_result # Step 5 print('hello world') # Step 6 sys.stdout = tmp # Step 7 print('VARIABLE:', my_result.getvalue()) # hello world

You can also check this out on our interactive Jupyter notebook so you don’t have to try in your own shell:

[Image: image-168.png]

[Image: 1f9e9.png] Try it yourself: Click to run in Jupyter Notebook (Google Colab)

References:

Are you ready to up your Python skills? Join our free email academy — we’ve cheat sheets too! [Image: 1f642.png]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: