Skip to content

Instantly share code, notes, and snippets.

@yogeshsinghgit
Created October 2, 2022 14:43
Show Gist options
  • Save yogeshsinghgit/8591b88b829d2b4b04f2619710ef541a to your computer and use it in GitHub Desktop.
Save yogeshsinghgit/8591b88b829d2b4b04f2619710ef541a to your computer and use it in GitHub Desktop.
writing content in a csv file using python csv module
# 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