Created
October 2, 2022 14:43
-
-
Save yogeshsinghgit/8591b88b829d2b4b04f2619710ef541a to your computer and use it in GitHub Desktop.
writing content in a csv file using python csv module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing library | |
import csv | |
def writeCSV(filename, columns_header, data): | |
with open(filename, mode='w') as csvfile: # opening file in write mode | |
writerObj = csv.writer(csvfile) # creating csv.writer object | |
writerObj.writerow(columns_header) # add columns value in csv | |
writerObj.writerows(data) # adding rows | |
columns_header = ['ID', "Name", "Age", "Gender"] | |
data = [ | |
[10, "Alex", 20, "M"], | |
[11,'Saloni', 21, 'F'], | |
[12, 'Dhruv', 21, 'M'] | |
] | |
writeCSV("filename.csv", columns_header, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment