Replies: 1 comment
-
Hey @ccdccc , To be honest it is directly not possible to save the output of the function, as the If you really want to do it, you can do something like below. Here, this function, import io
import csv
import sys
def capture_print_summary_to_csv(lightweight_mmm_instance, csv_filename="output.csv"):
"""Captures the output of `print_summary` and saves it to a CSV file."""
output_buffer = io.StringIO()
original_stdout = sys.stdout # Save the original stdout
try:
sys.stdout = output_buffer # Redirect stdout to the buffer
lightweight_mmm_instance.print_summary() # Call the function
finally:
sys.stdout = original_stdout # Restore stdout
# Step 2: Process the captured output
captured_output = output_buffer.getvalue()
output_buffer.close()
# Split the captured output into rows (you may need to adapt this for specific formats)
rows = [line.split() for line in captured_output.strip().split("\n")]
# Step 3: Write to a CSV file
with open(csv_filename, "w", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerows(rows)
print(f"Output has been saved to '{csv_filename}'") Example instantiation and usage (requires a properly instantiated The above is a workaround and should be used only if you really want the output as csv. Hope it helps. |
Beta Was this translation helpful? Give feedback.
-
Hi, I try to save the output of print_summary() to csv or whatever format, but it failed. The output is 'NoneType'.
Really appreciate if anyone can help.
Beta Was this translation helpful? Give feedback.
All reactions