CSV file read, write and search Theory — Lecture 36
CBSE | XII CS | Computational Thinking and Programming – 2 | 35 min
You are an expert CBSE XII CS Computer Science teacher, examiner, and study material creator. =========================================== SCOPE — READ BEFORE GENERATING ANYTHING =========================================== Today's lecture covers ONE topic only: "CSV file read, write and search" Lecture number 36 of 91 | Duration: 35 minutes | Board: CBSE Chapter: Computational Thinking and Programming – 2 HARD RULE: Every piece of content you generate — notes, examples, questions, tips — must be directly relevant to "CSV file read, write and search" only. DO NOT pull content, examples, or questions from any other topic or chapter. LECTURE MODE: THEORY / CONCEPT TEACHING - Teach the concept first: definitions, intuition, examples, syntax/steps, and misconceptions. - Use the given exam-frequency analysis internally to decide emphasis and short concept checks; keep the lecture concept-teaching focused. - Keep content tightly scoped to today's concept list. =========================================== SECTION 1: LECTURE INFORMATION =========================================== Class: XII CS | Subject: Computer Science | Board: CBSE Topic: CSV file read, write and search Theory Subtopics to cover today: - CSV file read, write and search Student level: Class XII, CBSE Board, average to above-average students preparing for board exams =========================================== SECTION 2: TEACHER'S REFERENCE NOTES =========================================== CSV file read, write and search Theory Concept ID: U1_CSV_OPS. Primary Sub-subtopic: CSV file read, write and search. Question-bank grouping: File Handling. Use this lecture for theory, examples, misconceptions, and short concept checks mapped to this concept ID. Teaching ideas: Teach the concept first, then use a small number of concept-ID matched concept checks. =========================================== SECTION 3: EXAM FREQUENCY DATA (Year-wise) =========================================== Teaching priority: HIGH High-yield concepts: CSV file read write and search, CSV file read write and search, Binary file operations with pickle, CSV file read write and search, Text file reading writing searching and counting, File basics paths modes seek and tell, CSV file read write and search | Year | Questions | Marks | |------|-----------|-------| | 2021 | 10 | 10 | | 2022 | 3 | 11 | | 2023 | 4 | 19 | | 2024 | 4 | 12 | | 2025 | 4 | 17 | | 2026 | 1 | 4 | | **Total** | **26** | **73** | =========================================== SECTION 4: EXAM ANALYSIS INPUT — DO NOT PRINT THESE IN THEORY NOTES (Scope: "CSV file read, write and search" only — 26 questions from board papers) =========================================== THEORY LECTURE RULE: use these PYQs only as private analysis data — this lecture is PURELY THEORY. - Cover every concept and trap revealed by these PYQs in the study notes - Do NOT print, quote, reproduce, paraphrase, or label any actual PYQ in the final HTML - Self-generate your own tricky/important questions and examples about the concept itself only — do not call them "PYQ-pattern", "PYQ-style", "board-style", or reference PYQs/previous years at all - In theory lectures, generated questions must be MCQ or Short Answer only - Never use the word "PYQ" or phrases like "previous year question" anywhere in the visible HTML --- 2021 Board Exam (10 questions | 10 marks) --- Q1. [MCQ] [1M] [Easy] Section-A Which of the following character acts as default delimiter in a csv file? a) (colon) : b) (hyphen) - c) (comma) , d) (vertical line) | Q2. [MCQ] [1M] [Easy] Section-A Syntax for opening Student.csv file in write mode is myfile = open("Student.csv","w",newline=''). What is the importance of newline=''? a) A newline gets added to the file b) Empty string gets appended to the first line. c) Empty string gets appended to all lines. d) EOL translation is suppressed Q3. [MCQ] [1M] [Easy] Section-A What is the correct expansion of CSV files? a) Comma Separable Values b) Comma Separated Values c) Comma Split Values d) Comma Separation Values Q4. [MCQ] [1M] [Easy] Section-A Which of the following is not a function/method of csv module in Python? a) read() b) reader() c) writer() d) writerow() Q5. [MCQ] [1M] [Easy] Section-C Rohit, a student of class 12, is learning CSV File Module in Python. During examination, he has been assigned an incomplete python code to create a CSV File 'Student.csv' with content: 1,AKSHAY,XII,A 2,ABHISHEK,XII,A 3,ARVIND,XII,A 4,RAVI,XII,A 5,ASHISH,XII,A Incomplete Code: import _____ #Statement-1 fh = open(_____, _____, newline='') #Statement-2 stuwriter = csv._____ #Statement-3 data = [] header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION'] data.append(header) for i in range(5): roll_no = int(input("Enter Roll Number : ")) name = input("Enter Name : ") Class = input("Enter Class : ") section = input("Enter Section : ") rec = [_____] #Statement-4 data.append(_____) #Statement-5 stuwriter._____(data) #Statement-6 fh.close() Identify the suitable code for blank space in the line marked as Statement-1. a) csv file b) CSV c) csv d) cvs Q6. [MCQ] [1M] [Easy] Section-C Identify the missing code for blank space in line marked as Statement-2. (Refer to the CSV file creation code in the case study: fh = open(_____, _____, newline='')) a) "Student.csv","wb" b) "Student.csv","w" c) "Student.csv","r" d) "Student.cvs","r" Q7. [MCQ] [1M] [Easy] Section-C Choose the function name (with argument) that should be used in the blank space of line marked as Statement-3. (Refer to the CSV file creation code in the case study: stuwriter = csv._____) a) reader(fh) b) reader(MyFile) c) writer(fh) d) writer(MyFile) Q8. [MCQ] [1M] [Easy] Section-C Identify the suitable code for blank space in line marked as Statement-4. (Refer to the CSV file creation code in the case study: rec = [_____]) a) 'ROLL_NO', 'NAME', 'CLASS', 'SECTION' b) ROLL_NO, NAME, CLASS, SECTION c) 'roll_no','name','Class','section' d) roll_no,name,Class,section Q9. [MCQ] [1M] [Easy] Section-C Identify the suitable code for blank space in the line marked as Statement-5. (Refer to the CSV file creation code in the case study: data.append(_____) ) a) data b) record c) rec d) insert Q10. [MCQ] [1M] [Easy] Section-C Choose the function name that should be used in the blank space of line marked as Statement-6 to create the desired CSV File. (Refer to the CSV file creation code in the case study: stuwriter._____(data)) a) dump() b) load() c) writerows() d) writerow() --- 2022 Board Exam (3 questions | 11 marks) --- Q1. [Assertion-Reason] [1M] [Easy] Section-A Assertion (A): CSV (Comma Separated Values) is a file format for data storage which looks like a text file. Reason (R): The information is organized with one record on each line and each field is separated by comma. a) Both A and R are true and R is the correct explanation for A b) Both A and R are true and R is not the correct explanation for A c) A is True but R is False d) A is false but R is True Q2. [Programming] [5M] [Hard] Section-D What is the advantage of using a csv file for permanent storage? Write a Program in Python that defines and calls the following user defined functions: (i) ADD() – To accept and add data of an employee to a CSV file 'record.csv'. Each record consists of a list with field elements as empid, name and mobile to store employee id, employee name and employee salary respectively. (ii) COUNTR() – To count the number of records present in the CSV file named 'record.csv'. (ii) search() – To display the records of the furniture whose price is more than 10000. Q3. [Programming] [5M] [Hard] Section-D Give any one point of difference between a binary file and a csv file. Write a Program in Python that defines and calls the following user defined functions: (i) add() – To accept and add data of an employee to a CSV file 'furdata.csv'. Each record consists of a list with field elements as fid, fname and fprice to store furniture id, furniture name and furniture price respectively. (ii) search() – To display the records of the furniture whose price is more than 10000. --- 2023 Board Exam (4 questions | 19 marks) --- Q1. [Programming] [4M] [Medium] Section-D 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. Q2. [Programming] [5M] [Hard] Section-D (a) Write one difference between CSV and text files. Write a program in Python that defines and calls the following user defined functions : (i) COURIER_ADD(): It takes the values from the user and adds the details to a csv file 'courier.csv'. Each record consists of a list with field elements as cid, s_name, Source, destination to store Courier ID, Sender name, Source and destination address respectively. (ii) COURIER_SEARCH() : Takes the destination as the input and displays all the courier records going to that destination. (ii) Search_Book(): Takes publisher name as input and counts and displays number of books published by them. Q3. [Programming] [5M] [Hard] Section-D (b) Why it is important to close a file before exiting ? Write a program in Python that defines and calls the following user defined functions : (i) Add_Book(): Takes the details of the books and adds them to a csv file 'Book.csv'. Each record consists of a list with field elements as book_ID, B_name and pub to store book ID, book name and publisher respectively. (ii) Search_Book(): Takes publisher name as input and counts and displays number of books published by them. Q4. [Programming] [5M] [Hard] Section-D (b) Write one point of difference between seek() and tell() functions in file handling. Write a program in Python that defines and calls the following user defined functions : (i) Add_Device() : The function accepts and adds records of the peripheral devices to a csv file 'peripheral.csv'. Each record consists of a list with field elements as P_id, P_name and Price to store peripheral device ID, device name, and price respectively. (ii) Count_Device() : To count and display number of peripheral devices, whose price is less than or equal to 1000. --- 2024 Board Exam (4 questions | 12 marks) --- Q1. [MCQ] [1M] [Easy] Section-A __________ files are stored in a computer in a sequence of bytes. (A) Text (B) Binary (C) CSV (D) Notepad Q2. [Programming] [3M] [Medium] Section-C Write the user defined function to modify the quantity of a product. The function should accept the product number, and the new quantity as arguments and update the quantity of that product in a CSV file "Product.csv". Q3. [Programming] [4M] [Medium] Section-D Mr. Mahesh is a Python Programmer working in a school. He has to maintain the records of the sports students. He has created a csv file named sports.csv, to store the details. The structure of sports.csv is : [sport_id, competition, prize_won] where sport_id, is Sport id (integer) competition is competition name (string) prize_won is ("Gold", "Silver", "Bronze") Mr. Mahesh wants to write the following user-defined functions : Add_detail() : to accept the detail of a student and add to a csv file, "sports.csv". Count_Medal() : to display the name of competitions in which students have won "Gold" medal. Help him in writing the code of both the functions. Q4. [Programming] [4M] [Medium] Section-D A csv file "Happiness.csv" contains the data of a survey. Each record of the file contains the following data: - Name of a country - Population of the country - Sample Size (Number of persons who participated in the survey in that country) - Happy (Number of persons who accepted that they were Happy) For example, a sample record of the file may be: ['Signiland', 5673000, 5000, 3426] Write the following Python functions to perform the specified operations on this file: (I) Read all the data from the file in the form of a list and display all those records for which the population is more than 5000000. (II) Count the number of records in the file. --- 2025 Board Exam (4 questions | 17 marks) --- Q1. [Programming] [4M] [Medium] Section-E Consider the table RESULT given below. | ROLL | SUBJECT | MARKS | |------|---------|-------| | S01 | Physics | 90 | | S02 | Chemistry | 98 | | S03 | Maths | 80 | | S04 | Physics | 95 | | S05 | Chemistry | 75 | | S06 | Maths | 97 | Write a program in Python to write the above data in a CSV file named RESULT.csv and display the records of all those students who are getting marks more than 80. Q2. [Programming] [4M] [Medium] Section-D Suman is an intern at a software startup. The company has assigned her a task to create a CSV file named CLUB.CSV, to store the records of the Club members. After discussing with Club Incharge, Suman has planned to store the following content of members in the file CLUB.CSV : [Mno, Name, Mobile, Fee] Where Mno – Member Number, Name – Name of the Member, Mobile – Member's Mobile Number, Fee – Fee amount Assuming you are asked to help Suman in her assignment, write a Python code for performing the following tasks with the help of user-defined functions : NewMembers() : to accept records of members from the user and add them to the file CLUB.CSV PriorityMember() : to find and display those members from the file CLUB.CSV, who are paying Fee more than 35000. Q3. [Programming] [4M] [Medium] Section-D Raj is the manager of a medical store. To keep track of sales records, he has created a CSV file named Sales.csv, which stores the details of each sale. The columns of the CSV file are: Product_ID, Product_Name, Quantity_Sold and Price_Per_Unit. Help him to efficiently maintain the data by creating the following user-defined functions: I. Accept() – to accept a sales record from the user and add it to the file Sales.csv. II. CalculateTotalSales() – to calculate and return the total sales based on the Quantity_Sold and Price_Per_Unit. Q4. [Programming] [5M] [Hard] Section-E Keshav is the IT Head in a hospital. He needs to manage the records of all the doctors in the hospital. For this, he wants to store the following information of each doctor in a file : D_ID – An integer to store Doctor ID. D_Name – A string to store doctor's name. D_Dept – A string to store the Department of the doctor. (Surgery, Radiology, etc.) Experience – An integer to store doctor's experience (in years) For example, a doctor's information may be : [1256, 'R. Gupta', 'Cardiology', 15] As an applicant for the post of a Programmer, you have to answer the following questions in this context : (I) Write one difference of storing this data in a binary file over a CSV file. (II) Assume that the data is stored in a binary file, named DOCTORS.DAT, and each record is stored as a list. Write a function, in Python, to read and display all the records from the file DOCTORS.DAT. (III) Write a function addDoctor(), in Python, which accepts a doctor's data from the user and writes it in the file DOCTORS.DAT. --- 2026 Board Exam (1 question | 4 marks) --- Q1. [Programming] [4M] [Medium] Section-D A csv file "States.csv" contains some data about all the states of India. Each record of the file contains the following data : · Name of the State · Capital of the State · Population of the State · Official Language of the State For example, a sample record in the file is : ['Andhra Pradesh', 'Amaravati', 52221000, 'Telugu'] Write a Python program which reads the data from this file and appends all those records where population is more than 10000000 into another csv file 'More.csv'. Note : "States.csv" also contains the Header row. The Header row should NOT be copied to "More.csv". =========================================== =========================================== QUESTION PATTERN BANK (What the board actually asks for THIS topic) =========================================== Scope: ONLY questions for today's lecture topic are listed below. DO NOT import questions from other topics or chapters. These are concept-pattern summaries (what TYPE the board asks), not copies of the actual questions — never reproduce full question text here. ### Concept: CSV file read write and search Pattern: Assertion-Reason=1, MCQ=9, Programming=10 | Marks: 1M=10, 3M=1, 4M=7, 5M=2 | Total: 20 questions [MCQ] [1M] [Easy] × 9 → Core concept of CSV file read write and search [Assertion-Reason] [1M] [Easy] → Core concept of CSV file read write and search [Programming] [5M] [Hard] × 2 → Core concept of CSV file read write and search [Programming] [4M] [Medium] × 7 → Core concept of CSV file read write and search [Programming] [3M] [Medium] → Core concept of CSV file read write and search ### Concept: CSV file read write and search, Binary file operations with pickle Pattern: MCQ=1, Programming=2 | Marks: 1M=1, 5M=2 | Total: 3 questions [MCQ] [1M] [Easy] → Core concept of CSV file read write and search, Binary file operations with pickle [Programming] [5M] [Hard] × 2 → Core concept of CSV file read write and search, Binary file operations with pickle ### Concept: CSV file read write and search, Text file reading writing searching and counting Pattern: Programming=1 | Marks: 5M=1 | Total: 1 questions [Programming] [5M] [Hard] → Core concept of CSV file read write and search, Text file reading writing searching and counting ### Concept: File basics paths modes seek and tell, CSV file read write and search Pattern: MCQ=1, Programming=1 | Marks: 1M=1, 5M=1 | Total: 2 questions [MCQ] [1M] [Easy] → Exception can occur even when syntax is correct [Programming] [5M] [Hard] → Core concept of File basics paths modes seek and tell, CSV file read write and search =========================================== =========================================== IMPORTANCE ANALYSIS (allocate teaching time by this ranking) =========================================== | Rank | Concept | Score | Times Tested | Total Marks | Recent Years | Priority | |------|---------|-------|-------------|-------------|--------------|----------| | 1 | CSV file read write and search | 136 | 20 | 51M | 2026, 2025, 2024 | CRITICAL | | 2 | CSV file read write and search, Binary file operations with pickle | 30 | 3 | 11M | 2025, 2022, 2021 | CRITICAL | | 3 | File basics paths modes seek and tell, CSV file read write and search | 16 | 2 | 6M | 2023, 2021 | HIGH | | 4 | CSV file read write and search, Text file reading writing searching and counting | 13 | 1 | 5M | 2023 | HIGH | CRITICAL concepts → full sub-section + comparison table + 2 worked examples HIGH concepts → 1 sub-section + 1 worked example MEDIUM concepts → definition + 1 quick example only =========================================== EXAMINER FINGERPRINT — TRAPS TO COVER INTERNALLY =========================================== Use the exam-frequency input only for internal analysis. Do not print, quote, reproduce, paraphrase, or label any actual board question in the final HTML. For this lecture, the generated teaching material must strongly cover these traps: No static trap list exists yet for "CSV file read, write and search". Self-generate 4–7 traps from: - actual question patterns for CSV file read write and search, CSV file read write and search, Binary file operations with pickle, CSV file read write and search, Text file reading writing searching and counting, File basics paths modes seek and tell, CSV file read write and search - common wrong assumptions students make about this concept - output-tracing traps - syntax-vs-runtime traps - comparison traps - order/sequence traps Important: this list should be treated as dynamic — for lectures with no static trap list, generate traps yourself from the categories above rather than leaving this section thin. =========================================== YOUR TASK — Generate a complete classroom-ready teaching package =========================================== Output format: FULL HTML (print-ready, A4, same format as CBSE study material). Use the CSS classes below. NO plain Markdown — use HTML elements only. HTML STRUCTURE TO GENERATE:
CBSE | XII CS | Computational Thinking and Programming – 2 | 35 min