Python 3 Create and Save CSV File Using CSV Module Full Tutorial For Beginners

Python 3 Create and Save CSV File Using CSV Module Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be creating and saving csv file using csv module in python 3 .The full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using pip command

 

pip install csv

 

Now make a app.py file and copy paste the following code

 

 

# Python program to demonstrate 
# writing to CSV 
    
    
import csv 
    
# field names 
fields = ['Name', 'Age', 'Subject', 'Hobbies'] 
    
# data rows of csv file 
rows = [ ['Nikhil', '23', 'Maths', 'Programming'], 
        ['Sanchit', '24', 'Computer Science', 'Cricket'], 
        ['Aditya', '34', 'Science', 'Basketball'], 
        ['Sagar', '37', 'Chemistry', 'Rugby'], 
        ['Prateek', '40', 'Physics', 'Football'], 
        ['Sahil', '45', 'Hindi', 'Politics']] 
    
# name of csv file 
filename = "university_records.csv"
    
# writing to csv file 
with open(filename, 'w') as csvfile: 
    # creating a csv writer object 
    csvwriter = csv.writer(csvfile) 
        
    # writing the fields 
    csvwriter.writerow(fields) 
        
    # writing the data rows 
    csvwriter.writerows(rows)

 

See also  Python 3 Tkinter Read Only Text Widget GUI Desktop App Full Project For Beginners

 

If you run this python file app.py and run python app.py

 

a csv file will be created in the same directory

 

 

 

 

 

Leave a Reply