after using print function, how can i get the print data
时间: 2023-04-11 10:00:42 浏览: 115
Advanced Apple Debugging & Reverse Engineering v0.9.5
You can capture the output of the print function by redirecting it to a variable using the "redirect_stdout" method from the "contextlib" module. Here's an example:
```
from contextlib import redirect_stdout
from io import StringIO
# create a StringIO object to capture the output
output = StringIO()
# redirect the output of the print function to the StringIO object
with redirect_stdout(output):
print("Hello, world!")
# get the captured output as a string
output_str = output.getvalue()
# print the captured output
print(output_str)
```
This will output:
```
Hello, world!
```
阅读全文