When append writing CSV do not write headers twice

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the data category.

Last Updated: 2024-11-21

I had some code to append logged model parameters for machine learning to a csv

def log_test_results(settings, error, train_test_error_difference):
    csv_path = 'test_results.csv'

    with open(csv_path, mode='a') as test_results:
        writer = csv.writer(
            test_results, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL
        )

        # Writing the header row
        writer.writerow(['Model', 'Data Description',
                         "Model Description", "Error",
                         "Train test error difference"
                         ])
        writer.writerow(
            [settings["Model"], settings["Data Description"],
                str(settings["Model Description"]),
                error, train_test_error_difference
             ]
        )

The problem was the headers appeared many times.

Here was the fix:

def log_test_results(settings, error, train_test_error_difference):
    csv_path = 'test_results.csv'
    # Add code to check if headers already existed
    must_add_headers = False if os.path.isfile(csv_path) else True

    with open(csv_path, mode='a') as test_results:
        writer = csv.writer(
            test_results, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL
        )

        if must_add_headers:
            writer.writerow(['Model', 'Data Description',
                             "Model Description", "Error",
                             "Train test error difference"
                             ])
        writer.writerow(
            [settings["Model"], settings["Data Description"],
                str(settings["Model Description"]),
                error, train_test_error_difference
             ]
        )