How to Write CSV File from List in Python?
I am going to show you example of python create csv file from list. I explained simply step by step python make csv file from list. you will learn how to create csv file from list in python. This post will give you simple example of python write csv file from list.
In this illustration, we'll make a demo.csv file with fields for ID, Name, and Email. A "data" list with values will be made. 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....