Professional Documents
Culture Documents
Code:
import csv
list1 = ["Enrollment No.", "Name", "Subject 1", "Subject 2", "Subject 3", "Subject 4", "Subject 5"]
list = []
for i in range(5):
marks = []
for j in range(5):
marks.append(subject_marks)
list.append(row)
writer = csv.writer(file)
writer.writerow(list1)
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
Code:
import csv
list1=["Emp No","Name","salary"]
list = []
for i in range(5):
if(salary>5000):
row=[EmpNo] +[name]+[salary]
else:
row=[EmpNo] +[name]
list.append(row)
writer = csv.writer(file)
writer.writerow(list1)
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]]
writer=csv.writer(file)
for i in list:
writer.writerow(i)
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
read = csv.DictReader(file)
for i in read:
print(i)
Output:
{'name': 'chirag', 'no': '1011'}
Que-5 Count the total number of records present in a CSV file and print it.
Code:
import csv
count=0
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
read = csv.reader(file)
for j in range(3):
print(a)
Output:
['chirag', '10', '9', '8', '7', '7']
contents of this file in python and display the total billing amount in
Python
Code:
import csv
count=0
read = csv.reader(file)
next(read)
for i in read:
count+=int(i[2])
Output:
bill is : 6200