Computer science
Computer science
csv files
CL ASS XII COMPUTER
SCIENCE
Learning Objectives
Introduction of CSV Files
file=open(‘famous.csv',’w’, newline=“”)
writer = csv.writer(file)
file.close()
Writing multiple rows
import csv
writer = csv.writer(file)
writer.writerows(row_list)
file.close()
Q: Write a program to add (append) Employee
records onto a csv
file.
import csv
with open('myfile.csv',mode = 'a') as csvfile:
mywriter = csv.writer(csvfile, delimiter=‘,’,
newline=“”)
ans = 'y' 101Deepak 38000
while ans.lower() == 'y':
121Raunak 58000
eno = int(input("Enter Employee Number:"))
name = input("Enter Employee Name:") 131Reena 80000
import csv
with open('famous.csv', 'r') as file:
reader = csv.reader(file, delimiter = '\t')
for row in reader:
print(row)
import csv
csvfile=open('book1.csv','r')
reader = csv.reader(csvfile,
skipinitialspace=True)
for row in reader:
print(row)
Programs on reader()
import csv
f=open("famous.csv", 'r')
count=0
reader=csv.reader(f, skipinitialspace=True)
for row in reader:
if reader.line_num==1:
continue
else:
print(row)
print("The number of rows are =", reader.line_num)
f.close()
Output:
['3', 'Aman', 'Humanities', '80']
['5', 'Gauri', 'Humanities', '93']
CSV_readSalary.py
Output:
['A12', 'Tarun Kumar', '28000']
['A15', 'Deepa', '25000']
['A16', 'Hemant', '29000']
(b) ..................... object contains the number of the current line in a CSV file.
(c) Every record in a CSV file is stored in reader object in the form of a list using
……………….. method?
(a) CSV module can handle CSV files correctly regardless of the operating system on
which the files were created.
(b) CSV module gets automatically imported into your program for reading a CSV file.
(c) Every record in a CSV file is stored in reader object in the form of a list.
newFile.______________ #Line 4
CBSE 2020 1*5 marks CSV
Ranjan Kumar of class 12 is writing a program to create a CSV file “user.csv” which will contain user name
and password for some entries. He has written the following code. As a programmer, help him to
successfully execute the given task.
import csv # Line 1
def addCsvFile(UserName,PassWord): CSV file
f=open(' user.csv’,’ ’a’) addCsvFile(“Arjun”,”123@456”)
# Line 2
addCsvFile(“Arunima”,”aru@nima”)
newFileWriter = csv.writer(f)
addCsvFile(“Frieda”,”myname@FRD
newFileWriter.writerow([UserName,PassWord])”) readCsvFile() #Line 5
f.close()
def readCsvFile():
with open(' user.csv','r') as newFile:
newFileReader = csv.reader(newFile) #Line 3
for row in newFileReader:
print (row[0],row[1])
Deepshikha Sethi ----XII Computer Science ----AIS Mayur Vihar
newFile.close() #Line 4
CSV 1*5 marks
The program needs to be be made to read the
file volume.csv and sum of open,high,low and
close needs to be done for each row and total
rows need to be counted
def readRecord():
cnt=0
with open(‘ volume.csv','r') as newFile: #Line 1
def readRecord():
cnt=0
with open(‘ volume.csv','r') as newFile: #Line 1
Each line contains a first name, a second name, a registration number, no of years and a
department separated by tabs.
1. Write a Python program that will copy the contents of the file into a list of tuples
2. Display full details of the student sorted by registration number
· The names of all students with no of year less than 3
· The number of people in each department