How to write a header row with CSV DictWriter in Python

Creating a header row with csv.DictWriter writes a header to a comma separated values (CSV) file from a dictionary of field names.

Solution for How to write a header row with CSV DictWriter in Python : You can use csv.DictWriter.writeheader() to write a header with CSV DictWriter Open the file in write mode with open(file_name, “w”) where file_name is the path to the file. Call csv.DictWriter(file=open_file, delimiter=”,”, fieldnames=headers) with open_file as the result of the previous step, delimiter as “,” because this file is a csv, and fieldnames as headers, where headers is a dict of desired headers for the file.


how-to-write-a-header-row-with-csv-dictwriter-in-python