In this article, you will get the important programs python CSV class 12. These programs can be used for your practical file. We have already given list of programs for:
Text Files – Click here to open
Binary Files – Click here to open
Topics Covered
Programs python CSV class 12
In this section of Programs python CSV class 12 we will read see 3 programs to read csv file contents and print them on output screen.
[1] Read a CSV file top5.csv and print the contents in a proper format. The data for top5.csv file are as following:
| SNo | Batsman | Team | Runs | Highest |
| 1 | K L Rahul | KXI | 670 | 132* |
| 2 | S Dhawan | DC | 618 | 106* |
| 3 | David Warner | SRH | 548 | 85* |
| 4 | Shreyas Iyer | DC | 519 | 88* |
| 5 | Ishan Kishan | MI | 516 | 99 |
from csv import reader
def pro1():
with open(“top5.csv”,”r”) as f:
d = reader(f)
data=list(d)
for i in data:
print(i)
pro1()
[2] Read a CSV file emp.csv and print the contents as it is without list brackets.
| EmpCode | EName | Post | Salary | Department |
| 1001 | Shobhit | Manager | 55000 | Finance |
| 1002 | Manmit | Clerk | 35000 | Office |
| 1003 | Sujal | Development Officer | 65000 | R & D |
| 1004 | Manisha | Tele Caller | 42000 | Communication |
| 1005 | Gayatri | System Analyst | 53000 | Software |
from csv import reader
def pro2():
with open(“emp.csv”,”r”) as f:
d = reader(f)
data=list(d)
for i in data:
print(‘, ‘.join(i))
pro2()
[3] Read a CSV file students.csv and print them with tab delimiter. Ignore first row header to print in tabular form.
from csv import reader
def pro3():
f = open(“students.csv”,”r”)
dt = reader(f,delimiter=’,’)
headr_row=next(dt)
data = list(dt)
f.close()
for i in data:
for j in i:
print(j,”\t”,end=” “)
print()
pro3()
In the next section, we will discuss how to write records in the CSV file using python code. Do practice for following programs python CSV class 12:
[4] Write records of customer into result.csv. The fields are as following:
| Field 1 | Data Type |
| StudentID | Integer |
| StudentName | String |
| Score | Integer |
from csv import writer
def pro4():
#Create Header First
f = open(“students.csv”,”w”,newline=’\n’)
dt = writer(f)
dt.writerow([‘Student_ID’,’StudentName’,’Score’])
f.close()
#Insert Data
f = open(“students.csv”,”a”,newline=’\n’)
while True:
st_id= int(input(“Enter Student ID:”))
st_name = input(“Enter Student name:”)
st_score = input(“Enter score:”)
dt = writer(f)
dt.writerow([st_id,st_name,st_score])
ch=input(“Want to insert More records?(y or Y):”)
ch=ch.lower()
if ch !=’y’:
break
print(“Record has been added.”)
f.close()
pro4()
The next program of programs python csv class 12 read contents as into dictionary object.
[5] Read and print above file contents in the form dictionary.
import csv
data = csv.DictReader(open(“students.csv”))
print(“CSV file as a dictionary:\n”)
for row in data:
print(row)
[6] Read Students name and score from the CSV file.
import csv
with open(‘students.csv’, newline=”) as f:
data = csv.DictReader(f)
print(“Student Name”,”\t”, “Score”)
print(“*”*50)
for i in data:
print(i[‘StudentName’], “\t”,i[‘Score’])
[7] Count the number of records and column names present in the CSV file.
Concepts used:
- Skip first row using next() method
- line_num properties used to count the no. of rows
import csv
def pro7():
fields = []
rows = []
with open(‘students.csv’, newline=”) as f:
data = csv.reader(f)
# Following command skips the first row of CSV file
fields = next(data)
print(‘Field names are:’)
for field in fields:
print(field, “\t”)
print()
print(“Data of CSV File:”)
for i in data:
print(‘\t’.join(i))
print(“\nTotal no. of rows: %d”%(data.line_num))
pro7()
Due to some technical reasons I couldn’t maintain the indentation in the python code properly. So download the code from below given link. Like our facebook page and download the codes.
More programs will be added and updated regularly. If you have any specific problem and feel something is missing in this article – programs python CSV class 12, let us know through comments.
Share this article as much as you can so more people will learn and practice these programs on programs python csv class 12.
I hope you enjoyed the practical part of the CSV file. Click on the provided link to find more codes and practice questions from the suggested and recommended links. Moreover, if you are looking for more contents on class 12 computer science using the below-given link: