Explore programming tutorials, exercises, quizzes, and solutions!
Python Working with CSV Exercises
1/20
You have a CSV file named data.csv containing the following content:
name,age
Alice,30
Bob,25
Charlie,35
Which of the following code snippets will correctly read and print each row as a dictionary using the built-in csv module?
Option 2 correctly uses csv.DictReader, which returns each row as a dictionary where keys are the column headers. Option 1 returns rows as plain lists, not dictionaries. Option 3 attempts to convert a list to a dictionary, which will raise an error. Option 4 tries to access a dictionary value by numeric index, which is invalid for a dictionary.
You want to write the following list of dictionaries to a CSV file:
Which code will correctly write this data into output.csv with headers?
Option 2 correctly uses csv.DictWriter with writeheader() to write the column names followed by each dictionary as a row. Option 1 uses writer, which expects a list of values, not a dictionary. Option 3 skips the header, which is essential. Option 4 tries to write dictionaries using writer, which will raise an error.
What does the newline="" parameter in the open() function do when working with CSV files?
When working with CSV files on Windows, opening the file with newline="" prevents extra blank lines from appearing between rows. Without it, the csv module would insert an additional \r for each line due to universal newline translation. It’s not for data corruption (Option 2), and doesn’t modify line endings directly (Option 4).
You have the following CSV file students.csv:
name,grade
Alice,A
Bob,B
Charlie,C
Which code snippet will load all grades into a list using csv.DictReader?
Option 1 uses csv.DictReader and accesses the "grade" field for each row properly. Option 2 uses csv.reader, which returns lists, so row["grade"] will raise a TypeError. Option 3 uses incorrect variable names inside the list comprehension. Option 4 tries to filter the header row incorrectly by comparing it to a string, and still uses csv.reader instead of DictReader.
What happens if you try to read a non-existent CSV file using open() without handling exceptions?
If the file does not exist and you attempt to open it in read mode ("r"), Python will raise a FileNotFoundError. This must be handled using a try-except block if you want to avoid crashing the program. Option 1 would happen only in write modes like "w". Option 2 is unrelated — KeyError occurs with dictionaries. Option 4 is incorrect; it will raise before the reader is even created.
You want to append a new row to an existing CSV file without overwriting its content. What is the correct way to open the file and append data?
new_row = {"name": "David", "age": 28}
To append to an existing CSV without overwriting, use mode "a" (append). Since you're writing a dictionary, use csv.DictWriter. Option 4 is correct and handles both the file mode and format properly. Option 1 incorrectly uses writer with a dictionary. Option 3 uses "w" mode, which would overwrite the file. Option 2 uses writer, which expects lists, and it misses the newline="" which may cause issues on Windows.
You are given the following CSV file products.csv:
Which code will read this file and convert all prices to floats, storing them in a list?
Option 2 correctly uses csv.DictReader to access the "price" field by key and converts it to float. Option 1 would work but includes the header row, which would raise an error trying to convert the string "price" to float. Option 3 incorrectly tries to subscript the reader object directly. Option 4 skips the header row but tries to access row[1] instead of row[2], leading to wrong data being parsed.
You want to write a CSV file with a custom delimiter (;) and quote all fields. Which code snippet correctly does that?
Option 1 is correct — it writes CSV data using ; as the delimiter and quotes every field using quoting=csv.QUOTE_ALL. Option 2 uses a different quote character but doesn't ensure all fields are quoted. Option 3 disables quoting entirely. Option 4 is invalid because writerows() does not take delimiter or quotechar as arguments — they must be passed to csv.writer() instead.
You're writing a script to update the salary of an employee in a CSV file. What is a correct and safe approach?
Option 3 correctly reads all rows into memory, modifies the required row, and rewrites the entire file. Since CSV files don't support in-place editing, rewriting is necessary. Option 1 appends a duplicate entry instead of updating. Option 2 incorrectly tries to read and write in the same loop over the file, which can lead to inconsistent behavior. Option 4 uses a writer on a file opened in read mode, which is not valid.
You have the following CSV file inventory.csv:
product,quantity
Laptop,5
Mouse,12
Keyboard,7
Which code will read it into a dictionary where the key is the product name and the value is the quantity as an integer?
Option 4 correctly uses csv.DictReader to access the header fields and casts quantity to an integer. Option 1 includes the header row and will fail converting "quantity" to int. Option 2 does not convert the quantity to an integer, which may cause type issues later. Option 3 skips the header but stores values as strings, not integers.
You want to merge two CSV files, file1.csv and file2.csv, both having the same headers, into a single CSV file merged.csv without duplicating the header. Which approach is correct?
Option 4 correctly writes all rows from the first file including the header, then skips the header from the second file using next(reader2) before writing its rows to avoid duplicating headers in the merged file. Option 1 duplicates headers from both files. Option 2 correctly skips the header but uses loops instead of the more efficient writerows(). Option 3 duplicates headers and uses writerows() without skipping.
You want to read a CSV file with irregular whitespace around the fields and remove the whitespace while reading. Which code snippet correctly processes this?
Option 4 correctly reads CSV rows as dictionaries and strips whitespace from each field value after reading. While skipinitialspace=True in options 1 and 3 helps remove spaces after delimiters, it does not strip trailing or leading spaces within quotes or other whitespace. Option 2 strips fields but uses csv.reader which returns lists, not dictionaries, so field names are not preserved.
You want to create a function filter_csv(input_file, output_file, column_name, threshold) that reads a CSV file, filters rows where the value in column_name is greater than threshold, and writes these rows to a new CSV file. Which implementation is correct?
Option 1 is correct — it properly reads CSV with DictReader, writes the header, and filters rows by converting the column value to int before comparison. Option 2 uses csv.reader which accesses columns by index, but tries to use column_name as an index, which is invalid. Option 3 forgets to write the header to the output file. Option 4 compares string values without casting to int, which can cause incorrect filtering.
Consider you want to read a CSV file with a custom delimiter ; and handle missing fields by filling them with a default value "N/A". Which code snippet achieves this correctly?
Option 3 explicitly checks each field in each row and replaces empty strings with "N/A", while reading with a custom delimiter ";" using DictReader. Option 1 uses a dictionary comprehension which works but may behave unexpectedly if fields contain empty strings versus None. Option 2 incorrectly assumes that empty rows can be replaced with "N/A" repeated, but it doesn't handle missing fields inside rows. Option 4 ignores the custom delimiter and processes a default CSV.
When using csv.DictReader in Python to read a CSV file with missing columns in some rows, what happens to those missing fields in the resulting dictionaries?
csv.DictReader assigns None to missing fields in rows that have fewer columns than expected. It does not raise exceptions for missing data nor fill with empty strings or custom values unless handled explicitly.
You receive a CSV file where the first row is missing headers, and you want to read it into a list of dictionaries by manually providing column names ["Name", "Age", "City"]. Which approach is correct?
When the CSV lacks headers, you can provide column names via the fieldnames parameter in DictReader. You should skip the first row if it contains data and not headers. Options 2 and 3 are less efficient or more error-prone. Option 4 is for writing CSVs, not reading.
You have a CSV file "records.csv" with the following content (fields separated by commas):
You want to read this file and create a dictionary mapping each id (int) to score (int), treating missing or invalid scores as 0. Which of the following functions implements this correctly?
Option 3 correctly uses try-except to convert score to int, setting invalid or missing scores to 0, and converts id to int as dictionary keys. Option 2’s isdigit() fails for empty strings and values like "not_available". Option 1 works but uses csv.reader (less clear with indices). Option 4 forgets to convert id to int for keys, which may cause inconsistent key types.
You want to write the following list of dictionaries to a CSV file, ensuring that fields containing commas are properly quoted:
data = [
{"name": "Alice", "comment": "Good, but needs improvement"},
{"name": "Bob", "comment": "Excellent!"},
{"name": "Charlie", "comment": "Average, could be better"}
]
Which code snippet correctly writes this data ensuring the commas inside comments don't break the CSV format?
Option 3 uses quoting=csv.QUOTE_ALL which ensures all fields are quoted, so commas inside fields like comments are safely handled. Option 1 and 2 use default or minimal quoting, which might not quote fields with commas, causing CSV parsing issues. Option 4 uses csv.writer and would require manual quoting; by default, it may not quote fields with commas properly.
Which of the following statements about the Python csv module are TRUE? (Choose the most accurate option)
Option 3 is true: Using newline='' when opening files for CSV operations is recommended to avoid problems with newline characters on different platforms. Option 1 and 2 are true to some extent but less precise. Option 4 is false because CSV files are flat, tabular text files and do not support nested data structures natively; such structures require special parsing or formats like JSON.
You receive CSV data from multiple sources, but some use semicolon (;) as delimiter instead of comma (,). Which approach will help you correctly parse all these files regardless of delimiter?
The csv.Sniffer class can analyze a sample of the CSV file and deduce the delimiter used, allowing you to pass the correct delimiter dynamically to csv.reader. Option 1 risks wrong parsing, option 3 may corrupt data if ; appears inside fields, and option 4 is outside the standard csv module and although useful, isn't always available or suitable.
Practicing Python Working with CSV? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Working with CSV
Welcome to the Python Working with CSV exercises — a comprehensive set of challenges designed to help you master handling CSV (Comma-Separated Values) files using Python. CSV is one of the most common file formats used for storing and exchanging tabular data across various applications, including spreadsheets, databases, and data analysis tools. Whether you’re a beginner or an experienced developer, these exercises will guide you through reading, writing, and manipulating CSV files effectively.
In this section, you’ll learn how to use Python’s built-in csv module to open, read, and write CSV files. You’ll practice parsing CSV data into Python lists or dictionaries and writing data back to CSV with proper formatting. These exercises will also cover handling common CSV-related challenges such as different delimiters, quoting characters, and line terminators.
Handling CSV files is essential for data scientists, analysts, and developers who work with large datasets or need to exchange information between systems. These exercises include practical examples such as processing sales data, managing user records, and transforming CSV content for analysis or reporting.
Mastering CSV handling in Python will prepare you for real-world projects involving data import/export, data cleaning, and integration with databases or APIs. Additionally, many technical interviews test candidates on file handling and data manipulation skills, making these exercises valuable for your career growth.
Alongside coding challenges, this section highlights best practices for working with CSV files, such as handling encoding issues, managing missing or malformed data, and efficiently processing large files. These insights will help you write reliable and maintainable Python code.
We recommend complementing your practice with related topics like file handling, Python dictionaries, and data analysis libraries such as pandas. Quizzes and multiple-choice questions on CSV handling are also available to help reinforce your understanding.
Start practicing the Python Working with CSV exercises today to build confidence in managing tabular data with Python. With consistent practice, you’ll be ready to tackle any CSV-related task in your projects with ease and efficiency.