You are on page 1of 5

Assignment_4

Que-1 Accept enrollno, name and marks of 5 subjects of 5 students from

user and write in a CSV file.

Code:
import csv

list1 = ["Enrollment No.", "Name", "Subject 1", "Subject 2", "Subject 3", "Subject 4", "Subject 5"]

list = []

for i in range(5):

name = input("Enter name of student :")

marks = []

for j in range(5):

subject_marks = int(input("Enter marks of subject :"))

marks.append(subject_marks)

row = [name] + marks

list.append(row)

with open('student.csv', mode='w', newline='') as file:

writer = csv.writer(file)

writer.writerow(list1)

for row in list:

writer.writerow(row)

Output:
Enrollment No.,Name,Subject 1,Subject 2,Subject 3,Subject 4,Subject 5

chirag,10,9,8,7,7

kishan,10,10,10,10,10

nupur,9,9,9,9,9

shradhdha,8,8,8,8,8

priyanshi,9,8,9,8,9
Que-2 Accept emp no, name and salary from user and write only those rows

in CSV file where salary is more than 5000.

Code:
import csv

list1=["Emp No","Name","salary"]

list = []

for i in range(5):

EmpNo=int(input("Enter emp number : "))

name = input("Enter name of student :")

salary=int(input("Enter your salary here :"))

if(salary>5000):

row=[EmpNo] +[name]+[salary]

else:

row=[EmpNo] +[name]

list.append(row)

with open('office.csv', mode='w', newline='') as file:

writer = csv.writer(file)

writer.writerow(list1)

for row in list:

writer.writerow(row)

Output:
Emp No,Name,salary

1011,chirag

1055,kishan,10000

1048,nupur

1036,shradhdha,15000

1044,sakshi,100000

Que-3 Write a Python list to a CSV file. After writing the CSV file, read the
CSV file and display the contents.

Code:
import csv

list=[["chirag",1011],

["kishan",1055]]

with open("bro.csv" , mode="w" , newline="") as file:

writer=csv.writer(file)

for i in list:

writer.writerow(i)

with open("bro.csv",mode="r") as file:

read = csv.reader(file)

for j in list:

print(j)

Output:

['chirag', 1011]

['kishan', 1055]

Que – 4 Write a CSV file to read any given CSV file as a dictionary

Code:
import csv

with open("dict.csv",mode="r") as file:

read = csv.DictReader(file)

for i in read:

print(i)

Output:
{'name': 'chirag', 'no': '1011'}

{'name': 'kishan', 'no': '1055'}

Que-5 Count the total number of records present in a CSV file and print it.
Code:
import csv

count=0

with open("bro.csv",mode="r") as file:

read = csv.reader(file)

for j in read:

count+=1

print(j)

print(count)

Output:
['chirag', '1011']

['kishan', '1055']

Que-6 Write a Python program to read first 3 rows (excluding the header) of

a CSV file.

Code:
import csv

count=0

with open("student.csv",mode="r") as file:

read = csv.reader(file)

next(read) #first time it returns header

for j in range(3):

a = next(read) #second time returns first row and so on

print(a)

Output:
['chirag', '10', '9', '8', '7', '7']

['kishan', '10', '10', '10', '10', '10']

['nupur', '9', '9', '9', '9', '9']


Que-7 Consider the below mentioned CSV file invoice.csv. Read the

contents of this file in python and display the total billing amount in

Python

Code:
import csv

count=0

with open("item.csv",mode="r") as file:

read = csv.reader(file)

next(read)

for i in read:

count+=int(i[2])

print("bill is : ", count)

Output:
bill is : 6200

You might also like