CSV File In Python


DIFFERENT TYPES OF CSV FILE PYTHON PROGRAMS :

  • PYTHON PROGRAM TO RECORD STUDENT DETAILS IN CSV FILE


1) PYHTON PROGRAM TO STORE STUDENT DETAILS IN CSV FILE

Q  WRITE A PYTHON PROGRAM TO CREATE A CSV FILE AND TO STORE NAME, ROLL NUMBER, STANDARD, DIVISION AND CGPA OF A STUDENT.


CODE FOR THE ABOVE  PYTHON  PROGRAM :



import csv
fields = ['Name','RollNo','Std','Div','CGPA',]
rows = [['NIKHIL','12','7','A','9.0'],
['SANCHIT','23','8','B','9.1'],
['ADITYA' ,'35','6','E','9.3'],
['SAGAR'  ,'24','8','A','9.1'],
['PRATEEK','15','7','C','7.8'],
['SAHIL'  ,'40','9','D','9.1']]

filename = "Students_records.csv"
with open(filename,'w')as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(fields)
    csvwriter.writerows(rows)


WRITTEN CODE FOR THE ABOVE PROGRAM :


import csv

fields = ['Name','RollNo','Std','Div','CGPA',]

rows = [['NIKHIL','12','7','A','9.0'],

['SANCHIT','23','8','B','9.1'],

['ADITYA' ,'35','6','E','9.3'],

['SAGAR'  ,'24','8','A','9.1'],

['PRATEEK','15','7','C','7.8'],

['SAHIL'  ,'40','9','D','9.1']]


filename = "Students_records.csv"

with open(filename,'w')as csvfile:

    csvwriter = csv.writer(csvfile)

    csvwriter.writerow(fields)

    csvwriter.writerows(rows)


OUTPUT :


NOTE : This CSV file python program prints the give value in the file "Students_records.csv". The program will automatically open the file and print the details give in the program. to check the details, you have to search the file named "Students_records" and open it. you will get the same output as given below. please make sure that (python would not open the csv file by itself, you have to search it in your computer search bar. )








Post a Comment

0Comments
Post a Comment (0)

Featured Highlighted Categories

You'll discover all of the most up-to-date bring innovative here.

Python List theory with List Programs
Get complete python lists theory and list basic and insane programs for your practice.
CBSE SAMPLE PAPERS
Get CBSE sample and question papers of last 7 years with answers to get best practice for your Board Exams
Python String theory and String Programs
Python strings complete theory with illustrations and practice programs for free