Python and CSV Files: One Mark Questions, Book Back Answers and Notes
This chapter shows how Python reads and writes CSV (Comma Separated Values) files. It covers how CSV differs from an Excel file, the rules for writing a CSV file, csv.reader and csv.writer, file modes, dialects, sorting by a column, and the DictReader and DictWriter classes.
Study this chapter
Asked in previous board exams
6 questions from this chapter came in 6 past public exams. Open any of them to see the answer and explanation.
-
March 2026
The expansion of CRLF is:See answer in the solved paper →
-
March 2025
Making some changes in the data of the existing file or adding more data is called:See answer in the solved paper →
-
March 2024
A CSV file is also known as a ________.See answer in the solved paper →
-
March 2023
The mode which is used when dealing with non-text files like image or exe files :See answer in the solved paper →
-
May 2022
The expansion of CRLF is :See answer in the solved paper →
-
March 2020
A CSV file is also known as a :See answer in the solved paper →
About Python and CSV Files
Medium ~90 min study
A CSV file is a plain text file where each line is one record and the fields are separated by commas. Almost every spreadsheet and database program can open or produce one, which is why it is used so often to move tables of data from one program to another. You can even create one in Notepad.
In this unit you connect Python to such a file with open(), read each row as a list using csv.reader(), and write rows with csv.writer(). You then handle harder cases: fields with spaces after the comma, fields containing quotes or commas, a different delimiter such as |, and files with a header row that you want to read as dictionaries.
Common questions ask how CSV differs from XLS, what each file mode means, the difference between writerow() and writerows(), and how DictReader works. Reading a file, sorting it on one column and appending a row are the three textbook programs worth typing out and running yourself, because small slips like a wrong mode or a missing import only show up when the code runs.
What you'll learn
- State the differences between a CSV file and an XLS file.
- Create a correctly formatted CSV file in a text editor, including fields with commas and quotes.
- Read a CSV file into a Python program using open() and csv.reader().
- Use skipinitialspace and registered dialects to read files with extra spaces or a different delimiter.
- Write and append rows with csv.writer(), writerow() and writerows().
- Sort rows on a chosen column using sorted() and operator.itemgetter().
Before you start
- Python variables, loops and basic file handling.
- Lists, tuples and dictionaries.
- Basic spreadsheet ideas: rows, columns and header rows.
Topics covered in this chapter
Python and CSV Files explained
Working with CSV Files in Python
CSV Files and Excel Files
A CSV file stores tabular data as plain text, so any text editor can open it. An Excel (XLS) file is a binary file that holds all the worksheets of a workbook along with their content and formatting. Excel files can be read only by programs written to read that format, and they take more memory when data is imported. CSV files are smaller and can be moved between programs easily.
Rules for Writing a CSV File
Each record is on its own line, ended by a line break. The last record may or may not have a line break after it. An optional header line with field names can come first. Fields are separated by commas. A field that contains a comma, a double quote or a line break must be enclosed in double quotes, and a double quote inside such a field is written twice, so "Red" Rose becomes """Red"" Rose".
Reading a CSV File with csv.reader()
First import the csv module and open the file with open(). Using with open(...) as f: closes the file automatically when the block ends. csv.reader(f) returns a reader object; looping over it gives each row as a list of strings. You can pick out one column from each row and append it to a list for sorting or other processing.
Dialects and skipinitialspace
By default the reader treats a space after a comma as part of the next field. Setting skipinitialspace=True removes those leading spaces. When a file uses a different layout, you can register a dialect with csv.register_dialect(), giving settings such as delimiter='|', quotechar or quoting=csv.QUOTE_ALL, and then pass that dialect name to the reader or writer.
Writing and Appending Rows
To write, open the file in 'w' mode and create a writer with csv.writer(). Mode 'w' creates the file or overwrites whatever was in it, while 'a' adds new rows at the end without touching the old ones. writerow() writes one row from a list; writerows() writes many rows from a list of lists. The lineterminator setting decides what ends each line.
DictReader and DictWriter
csv.DictReader uses the first row of the file as keys and returns each later row as a dictionary, so you can write row['Name'] instead of row[0]. The textbook output shows these rows as OrderedDict; newer Python versions (3.8 onwards) return plain dict. csv.DictWriter takes a fieldnames list, writes the header with writeheader(), and writes dictionaries as rows.
Common mistakes to avoid
- Opening a file without with and then forgetting close(). Use a with block so the file is closed automatically.
- Missing a comma or adding stray spaces when typing records in a text editor. Each field must be separated by exactly one comma, or the columns shift.
- Doing arithmetic on values read from a CSV file without converting them. csv.reader gives strings, so use int() or float() first.
- Opening a file in 'w' mode when you only wanted to add rows. 'w' wipes the existing content; use 'a' to append.
- Dropping an empty field when writing a row by hand. Write two commas side by side so the columns after it stay in the right place.
Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.
Frequently asked questions
How is a CSV file different from an Excel file?
A CSV file is plain text: fields separated by commas, one record per line, no formatting. An Excel file is a binary file that stores worksheets with their content and formatting. It can be opened only by programs that understand the format and uses more memory when importing data.
Why use the with statement to open a file?
A with block closes the file automatically when the block finishes, even if an error occurs inside it. You do not have to remember to call close().
How do I store a field that contains a comma?
Enclose the whole field in double quotes, for example "Chennai, Tamil Nadu". The reader then treats the comma as part of the text, not as a separator. A double quote inside a quoted field is written as two double quotes.
What does skipinitialspace do?
By default a space after a comma is kept as part of the next field, so you get values like ' Chennai'. With skipinitialspace=True, either passed to csv.reader or set in a registered dialect, those leading spaces are removed.
What is the difference between 'w' and 'a' modes?
'w' creates a new file, or empties an existing one before writing. 'a' opens the file and writes new rows after the existing ones, so nothing already in the file is lost.
When should I use DictReader instead of csv.reader?
Use csv.reader when working with rows as lists by position is enough. Use DictReader when the file has a header row, because each row comes back as a dictionary and you can refer to a field by its column name.
How do I sort CSV data by a particular column?
Read the rows into a list, then use sorted(data, key=operator.itemgetter(1)) to sort on column 1. Add reverse=True for descending order. Remember to import the operator module, and skip the header row if the file has one.
Last updated 15 September 2026