How to Write Header in CSV File using Python?
In this post, we will learn create csv file with column names python. you will learn python add header to csv file. this example will help you python add header to csv if not exists. you'll learn how to add header in csv file using python. follow bellow step for python append header to csv.
In this tuts, i will make a demo.csv file with fields for ID, Name, and Email. In order to write numbers of rows to a csv file, we will create a "data" list with values. To build a CSV file from a list, we will utilise the open(), writer(), writerow(), writerows(), and close() functions.
Without further ado, let's look at the code example below:
So let's see bellow example:
import csv # open the file in the write mode f = open('demo.csv', 'w') # create the csv writer writer = csv.writer(f) header = ['ID', 'Name', 'Email'] data = [ [1, 'Bhavesh Sonagra', '[email protected]'], [2, 'Nikhil Patel', '[email protected]'], [3, 'Piyush Kamani', '[email protected]'] ] # write the header writer.writerow(header) # write a row to the csv file writer.writerows(data) # close the file f.close()Output
It will help you....