0% found this document useful (0 votes)
79 views6 pages

CSV File Programs

The document contains several Python programs that demonstrate how to create, write, read, and manipulate CSV files. It includes examples for storing student data, competition results, employee information, user accounts, and sports event results. Each program is designed to perform specific tasks such as writing records, suppressing end-of-line translations, and searching for user passwords.

Uploaded by

thanoshu55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views6 pages

CSV File Programs

The document contains several Python programs that demonstrate how to create, write, read, and manipulate CSV files. It includes examples for storing student data, competition results, employee information, user accounts, and sports event results. Each program is designed to perform specific tasks such as writing records, suppressing end-of-line translations, and searching for user passwords.

Uploaded by

thanoshu55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSV FILE PROGRAMS

1. WAP to create a CSV file to store student data (Roll No., Name, Marks). Obtain
data from the user and write 5 records into the file.

1 import csv
2
3 com = open ("Student.csv", "w")
4
5 w = csv.writer (com)
6 w.writerow (['Rollno', 'Name', 'Marks'])
7
8 for i in range (5) :
9 print ("Student record", (i + 1))
10 roll = int (input ("Enter roll no. : "))
11 name = input ("Enter name : ")
12 marks = int (input ("Enter marks : "))
13
14 sturec = [roll, name, marks]
15
16 w.writerow (sturec)
17
18 com.close ()
2. The data of winners of four rounds of a competitive programming competition is
given as :
['Name', 'Points', 'Rank']
['Shradha', 4500, 23]
['Nishchay', 4800, 31]
['Ali', 4500, 25]
['Adi', 5100, 14]
WAP to create a csv file (compresult.csv) and write the above data into it.

1 import csv
2
3 res = open ("compresult.csv", "w")
4
5 cw = csv.writer (res)
6
7 compdata = [['Name', 'Points', 'Rank'],
8 ['Shradha', 4500, 23],
9 ['Nishchay', 4800, 31],
10 ['Ali', 4500, 25],
11 ['Adi', 5100, 14]]
12
13 cw.writerows (compdata)
14 res.close ()

3. WAP to read records of the csv file created in the previous program and display
them

1 import csv
2
3 with open ("compresult.csv", "r") as comp :
4 creader = csv.reader (comp)
5
6 for rec in creader :
7 print (rec)
4. The csv file in the previous program was created on Windows where EOL
character is '\r\n'. Modify the code of the previous program so that blank lines for
every EOL are not displayed

1 import csv
2
3 with open ("compresult.csv", "r", newline = '\r\n') as comp :
4 creader = csv.reader (comp)
5
6 for rec in creader :
7 print ("No. of records : ", len(rec))
8 print (rec)

5. WAP to create a csv file by suppressing EOL translation.

1 import csv
2
3 emp = open ("Employee.csv", "w", newline = "")
4
5 ew = csv.writer (emp)
6
7 empdata = [['Empno', 'Name', 'Designation', 'Salary'],
8 [1001, 'Trupti', 'Manager', 56000],
9 [1002, 'Raziya', 'Manager', 55900],
10 [1003, 'Simran', 'Analyst', 35000],
11 [1004, 'Silviya', 'Clerk', 25000],
12 [1005, 'Suji', 'PR Officer', 31000]]
13
14 ew.writerows (empdata)
15 print ("File successfully created")
16
17 emp.close ()
6. Create a CSV file by entering user-id and password, read and search the
password for given user-id.

1 import csv
2 def Create () :
3 acc = open ("Account.csv", "w")
4
5 ac_wr = csv.writer (acc)
6 ac_wr.writerow (['User-Id', 'Password'])
7
8 size = int (input ("Enter number of records : "))
9
10 for i in range (size) :
11 print ("Detail no.", (i + 1))
12 uid = input ("Enter User Id. : ")
13 pswd = input ("Enter Password : ")
14
15 accrec = [uid, pswd]
16
17 ac_wr.writerow (accrec)
18
19 acc.close ()
20
21 def Search () :
22 with open ("Account.csv", "r") as ac :
23 ac_re = csv.reader (ac)
24
25 use = input ("Enter User-id to be searched : ")
26
27 x = ''
28 for rec in ac_re :
29 if use in rec :
30 x = rec [1]
31 else :
32 continue
33
34 if len (x) == 0 :
35 print ("Record not found.")
36 else :
37 print ("Password for", use, ":", x)
7. Vedansh is a Python programmer working in a school. For the Annual Sports
Event, he has created a csv file named Result.csv, to store the results of students in
different sports events. The structure of Result.csv is :
[St_Id, St_Name, Game_Name, Result]
where,
St_Id is Student ID (integer)
ST_name is Student Name (string)
Game_Name is name of game in which student is participating (string)
Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'

For efficiently maintaining data of the event, Vedansh wants to write the following
user defined functions:

Accept() – to accept a record from the user and add it to the file Result.csv. The
column headings should also be added on top of the csv file.

wonCount() – to count the number of students who have won any event. As a
Python expert, help him complete the task.

1 import csv
2 def Accept () :
3 f = open (“Result.csv”, “a”, newline = “”)
4 cw = csv.writer (f)
5
6 heading = [“St_ID”, “St_Name”, “Game_Name”, “Result”]
7 cw.writerow (heading)
8 size = int (input ("Enter number of records : "))
9
10 for i in range (size) :
11 sid = int(input ("Enter student id. : "))
12 sname = input ("Enter Student Name : ")
13 game = input ("Enter name of game : ")
14 res = input ("Enter Result : ")
15
16 data = [sid, sname, game, res]
17
18 cw.writerow (data)
19
20 f.close ()
21
22
23
24 def wonCount () :
25 f = open (“Result.csv”, “r”)
26 head = next (f)
27 cr = csv.reader (f)
28
29 print (head)
30
31 for x in cr :
32 if x[3] == “WON” :
33 print (x)
34 f.close ()

You might also like