Working with CSV Files: book back answers
19 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Working with CSV FilesChoose the best answer (1 Mark)
A CSV file is also known as a ….
- (A) Flat File
- (B) 3D File
- (C) String File
- (D) Random File
Try it, then show the answerAnswer
(A) Flat File
The chapter states that a CSV file is also known as a Flat File.
From the book: “A CSV file is also known as a Flat File.”
Which of the following module is provided by Python to do several operations on the CSV files?
- (A) py
- (B) xls
- (C) csv
- (D) os
Try it, then show the answerAnswer
(C) csv
Python provides a module named CSV for operations on CSV files.
From the book: “Python provides a module named CSV, using this you can do several operations on the CSV files.”
Which of the following mode is used when dealing with non-text files like image or exe files?
- (A) Text mode
- (B) Binary mode
- (C) xls mode
- (D) csv mode
Try it, then show the answerAnswer
(B) Binary mode
Binary mode returns bytes and is used for non-text files such as image or exe files.
From the book: “this is the mode to be used when dealing with non-text files like image or exe files.”
The command used to skip a row in a CSV file is
- (A) next()
- (B) skip()
- (C) omit()
- (D) bounce()
Try it, then show the answerAnswer
(A) next()
The chapter's example skips the first row by calling next(reader).
From the book: “# skipping the first row(heading) next(reader)”
Which of the following is a string used to terminate lines produced by writer()method of csv module?
- (A) Line Terminator
- (B) Enter key
- (C) Form feed
- (D) Data Terminator
Try it, then show the answerAnswer
(A) Line Terminator
The chapter defines a Line Terminator as the string used to end lines produced by the writer.
From the book: “A Line Terminator is a string used to terminate lines produced by writer.”
Which of the following creates an object which maps data to a dictionary?
- (A) listreader()
- (B) reader()
- (C) tuplereader()
- (D) DictReader ()
Try it, then show the answerAnswer
(D) DictReader ()
DictReader creates an object that maps CSV data to a dictionary.
From the book: “DictReader method of csv module which works similar to the reader() class but creates an object which maps data to a dictionary.”
Making some changes in the data of the existing file or adding more data is called
- (A)Editing
- (B) Appending
- (C)Modification
- (D) Alteration
Try it, then show the answerAnswer
(C)Modification
The chapter calls changing existing data or adding more data modification.
From the book: “Making some changes in the data of the existing file or adding more data is called modification”
Part - II — Answer the following questions (2 Marks)
What is CSV File?
Try it, then show the answerAnswer
A CSV file is a human-readable text file in which each line has fields separated by commas or another delimiter. Each line can be treated as a row and each field as a column.
The chapter describes CSV files as plain text files used for tabular data.
From the book: “A CSV file is a human readable text file where each line has a number of fields, separated by commas”
Mention the two ways to read a CSV file using Python.
Try it, then show the answerAnswer
Using the csv.reader() function and the DictReader class.
The chapter lists these as the two ways to read a CSV file.
From the book: “Two ways of Reading CSV File reader () function Dict Reader class”
Mention the default modes of the File.
Try it, then show the answerAnswer
The default mode is read text mode, written as 'r' or 'rt'.
When no mode is specified, the chapter says the default is rt, or reading in text mode.
From the book: “the default mode rt is used”
What is use of next() function?
Try it, then show the answerAnswer
next() is used to skip the first row, such as a heading row, when reading a CSV file.
The chapter's example calls next(reader) to omit the heading before processing the remaining rows.
From the book: “# skipping the first row(heading) next(reader)”
How will you sort more than one column from a csv file?Give an example statement.
Try it, then show the answerAnswer
Use operator.itemgetter() with multiple indices. Example: operator.itemgetter(1,2).
The chapter says multiple indices can be supplied to itemgetter to sort by more than one column.
From the book: “To sort by more than one column you can use itemgetter with multiple indices: operator .itemgetter (1,2)”
Part - III — Answer the following questions (3 Marks)
Write a note on open() function of python. What is the difference between the two methods?
Try it, then show the answerAnswer
The built-in open() function opens a file and returns a file object, also called a handle. Its mode can specify reading ('r'), writing ('w'), or appending ('a'), and text or binary access. Opening a file directly requires closing it explicitly; using the with statement closes it automatically when the block ends.
The chapter describes both direct open-and-close file handling and the safer with statement method.
From the book: “The best way to do this is using the “with” statement. This ensures that the file is closed when the block inside with is exited.”
What is the difference between the write mode and append mode.
Try it, then show the answerAnswer
Write mode ('w') creates a new file and overwrites an existing file. Append mode ('a') adds data to the end of an existing file; if the file does not exist, it creates one.
The two modes differ in whether they replace existing file contents or add data at the end.
From the book: “‘w’ write mode creates a new file. If the file is already existing ‘w’ mode over writs it. Where as ‘a’ append mode add the data at the end”
What is the difference between reader()method and DictReader() class?
Try it, then show the answerAnswer
csv.reader() works with list/tuple data and returns rows as lists. csv.DictReader() creates an object that maps each row to a dictionary, using field names as keys.
The chapter distinguishes the methods by the type of data structure they use.
From the book: “csv. reader and csv.writer work with list/tuple, while csv.DictReader and csv.DictWriter work with dictionary.”
Part - IV — Answer the following questions (5 Marks)
Differentiate Excel file and CSV file.
Try it, then show the answerAnswer
Excel and CSV are different file formats used to store tabular information. They differ in how they store data, which programs can open them, and how much memory they use.
- File format: Excel is a binary file, while CSV is a plain text format with values separated by commas.
- Data stored: An Excel file can hold information about all worksheets, including content and formatting. A CSV file stores tabular information as delimited text.
- Opening and editing: CSV files can be opened with text editors as well as spreadsheet programs. Excel files cannot be opened or edited by text editors.
- Application support: XLS files can only be read by applications specifically written to read their format.
- Memory and speed: Excel consumes more memory while importing data. Importing CSV files can be faster and uses less memory.
- CSV limitations: CSV stores data but does not contain formatting, formulas, or macros.
Example: A spreadsheet can be saved in CSV format by selecting File → Save As and choosing CSV (Comma delimited).
Thus, Excel supports worksheets and formatting, whereas CSV is a simpler, text-based format for tabular data.
From the book: “CSV is a simple file format used to store tabular data, such as a spreadsheet or database.”
Tabulate the different mode with its meaning.
Try it, then show the answerAnswer
A mode specifies how a file is opened for an operation. The chapter describes the following file modes:
- Read mode ('r'): Opens a file for reading.
- Write mode ('w'): Opens a file for writing. If the file already exists, it is overwritten.
- Append mode ('a'): Adds data at the end of an existing file; if the file does not exist, it creates a new one.
- Text mode ('t'): Opens a file in text mode and is the default mode.
- Binary mode ('b'): Opens a file in binary mode, which is used for non-text files.
Example: The chapter gives the example: f = open("sample.txt",'w') # write in text mode
Thus, the mode chosen determines how a file is opened and used.
From the book: “In mode, you can specify whether you want to read 'r', write 'w' or append 'a' to the file.”
Write the different methods to read a File in Python.
Try it, then show the answerAnswer
Python provides two ways to read a CSV file: the reader() function and the DictReader class. Both are available through Python’s CSV module.
- Using csv.reader(): The reader() function reads each line and makes a list of its columns. You can then select the column values you need.
- Using DictReader: The DictReader class reads the first line as dictionary keys and maps the values in later rows to those keys.
- Storing rows in a list: Rows read from a CSV file can be added to a list using append().
- Reading a column into a list: A selected column can be read and stored in a list, for example to sort its values.
- Using a custom delimiter: For a CSV file with a custom delimiter, register a dialect and use it while reading the file.
Example: The chapter reads “sample1.csv” row by row using reader = csv.reader(F) and prints each row.
Thus, CSV files can be read as rows, dictionaries, selected columns, or list data using the methods described in the chapter.
From the book: “The two ways to read a CSV file are using csv.reader() function and using DictReader class.”
Write the rules to be followed to format the data in a CSV file.
Try it, then show the answerAnswer
A CSV file stores tabular data as records (rows) and fields (columns). The following rules should be followed when formatting its data.
- Separate records: Place each record on a separate line, using a line break made by pressing Enter.
- Final line break: The last record in the file may have an ending line break or may not have one.
- Optional header: An optional header can appear as the first line. It must have the same format and number of fields as the other records.
- Header contents: The header contains names corresponding to the fields in the file.
- Separate fields: Separate fields within the header and each record with commas. Spaces are part of a field and should not be ignored.
- Last field: Do not place a comma after the last field in a record.
Example: Red , Blue
Following these rules formats CSV data consistently as records and comma-separated fields.
From the book: “Spaces are considered part of a field and should not be ignored. The last field in the record must not be followed by a comma.”