top of page

import csv

def export_to_csv(data, filename="output.csv"):
    """
    Exports data to a CSV file.
    
    Args:
        data (list of dict): The data to export.
        filename (str): The name of the CSV file.
    """

    if not data:
        print("No data to export.")
        return

    with open(filename, mode="w", newline="") as file:
        # Automatically derive fieldnames from the first dictionary entry
        writer = csv.DictWriter(file, fieldnames=data[0].keys())
        
        # Write header and data
        writer.writeheader()
        writer.writerows(data)

    print(f"Data has been saved to {filename}")
 

Module
Export CSV

bottom of page