Python Program for CSV Writer

Python Program for CSV File Writer

CSV means comma separated values and it is one of the most common format to import and export in spreadsheets and databases.

csv module has the functionalities to read or write the CSV format data.

CSV file delimiter must be a single character suppose csv file uses different character instead of ‘,’.

Python Program for CSV Writer

This python program is used to create CSV format data file from list of each line as array of fields value.

import csv                                                                      
                                                                                
rows =[['id','name','class'],[1,'student1','VII'], [2,'student2','XII'], [3,'student3','VIII']]
                                                                                
with open('test-file.csv', 'w') as csv_file:                                    
    csv_obj = csv.writer(csv_file)                                              
    csv_obj.writerows(list(rows))                                               
                                                                                
print("CSV file is created!") 
Output:
$ python csv-writer.py
CSV file is created!
$ cat test-file.csv
id,name,class
1,student1,VII
2,student2,XII
3,student3,VIII

Python Program for CSV DictWriter

This python program is used to create CSV file using python dictionary data into CSV format data.

import csv                             

rows =[{'id': 1, 'name': 'student1', 'class': 'VII'},                           
       {'id': 2, 'name': 'student2', 'class': 'XII'},                           
       {'id': 3, 'name': 'student3', 'class': 'VIII'}]   

with open('test-file.csv', 'w') as csv_file:                                    
    csv_obj = csv.DictWriter(csv_file, fieldnames=['id', 'name', 'class'])      
    csv_obj.writeheader()                                                       
    csv_obj.writerows(rows)                                                     
                                                                                
print("CSV file is created!")  
Output:
$ python csv-writer1.py
CSV file is created!
$ cat test-file.csv
id,name,class
1,student1,VII
2,student2,XII
3,student3,VIII

Python Program for CSV Writer with Custom Delimiter

This python program is used to create the CSV file with custom delimeter character.

delimiter = ';',

Also creates each field value is highlighted with quote character.

quotechar=’”’

Below code uses register_dialect method to set the attributes to create the CSV file, this method is also used with csv writer function.

import csv      

rows =[{'id': 1, 'name': 'student1', 'class': 'VII'},                           
       {'id': 2, 'name': 'student2', 'class': 'XII'},                           
       {'id': 3, 'name': 'student3', 'class': 'VIII'}]      

csv.register_dialect('customDialect', delimiter = ';', quotechar = '"',         
                     quoting=csv.QUOTE_ALL, skipinitialspace=True)      

with open('test-file.csv', 'w') as csv_file:                                    
    csv_obj = csv.DictWriter(csv_file, fieldnames=['id', 'name', 'class'], dialect='customDialect')
    csv_obj.writeheader()                                                       
    csv_obj.writerows(rows)                                                     
                                                                                
print("CSV file is created!") 
Output:
$ python csv-writer1.py
CSV file is created!
$ cat test-file.csv
"id";"name";"class"
"1";"student1";"VII"
"2";"student2";"XII"
"3";"student3";"VIII"

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^