Python OOPs


Python SQLite


Examples


Others


Python List to CSV File


The csv module in Python's standard library used to read and write CSV files. The following examples shows, how to export list values into a csv file in Python.

Example
import csv 

#creating a list for column headers 
headers=['NAME','AGE','CONTACT']

#creating a list for rows
rows=[
    ['Ram',25,'9632587410'],
    ['Sam',30,'9632587412'],
    ['Tom',40,'9632587414'],
]

with open("sample.csv",'w', newline='') as csvFile:
    #creating a CSV writer object
    wr = csv.writer(csvFile)
    
    #writing headers
    wr.writerow(headers)
    
    #writing rows 
    wr.writerows(rows)
    
print("Complete")