Bird
Raised Fist0
Pythonprogramming~10 mins

Working with CSV files in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a CSV file named 'data.csv' for reading.

Python
with open('data.csv', [1]) as file:
    print(file.read())
Drag options to blanks, or click blank then click option'
A'r'
B'w'
C'a'
D'x'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' will overwrite the file.
Using 'a' opens for appending, not reading.
2fill in blank
medium

Complete the code to import the module needed to work with CSV files.

Python
[1] csv
Drag options to blanks, or click blank then click option'
Afrom
Busing
Crequire
Dimport
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'from' without specifying what to import.
Using 'require' which is not Python syntax.
3fill in blank
hard

Fix the error in the code to read rows from a CSV file using csv.reader.

Python
with open('data.csv', 'r') as file:
    reader = csv.[1](file)
    for row in reader:
        print(row)
Drag options to blanks, or click blank then click option'
Areader
Bread_csv
Creadlines
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'read' which is a file method, not csv module.
Using 'read_csv' which is from pandas, not csv module.
4fill in blank
hard

Fill both blanks to write rows to a CSV file using csv.writer.

Python
with open('output.csv', [1]) as file:
    writer = csv.[2](file)
    writer.writerow(['Name', 'Age'])
Drag options to blanks, or click blank then click option'
A'w'
B'r'
Cwriter
Dreader
Attempts:
3 left
💡 Hint
Common Mistakes
Opening file in 'r' mode when writing.
Using csv.reader() instead of csv.writer().
5fill in blank
hard

Fill all three blanks to create a dictionary from CSV rows using csv.DictReader and print the 'Name' field.

Python
with open('data.csv', [1]) as file:
    reader = csv.[2](file)
    for row in reader:
        print(row[[3]])
Drag options to blanks, or click blank then click option'
A'r'
BDictReader
C'Name'
D'w'
Attempts:
3 left
💡 Hint
Common Mistakes
Opening file with 'w' when reading.
Using csv.reader instead of DictReader.
Trying to access row with wrong key.

Practice

(1/5)
1. What does the Python csv.reader function do when working with CSV files?
easy
A. Reads the CSV file and returns each row as a list of values
B. Writes data to a CSV file
C. Deletes a CSV file
D. Converts CSV data into JSON format

Solution

  1. Step 1: Understand the purpose of csv.reader

    The csv.reader function reads CSV files and returns each row as a list of strings representing the columns.
  2. Step 2: Differentiate from other CSV functions

    Functions like writing or deleting files are not done by csv.reader. It only reads and parses rows.
  3. Final Answer:

    Reads the CSV file and returns each row as a list of values -> Option A
  4. Quick Check:

    csv.reader reads rows as lists [OK]
Hint: Remember: reader reads rows as lists [OK]
Common Mistakes:
  • Confusing reader with writer
  • Thinking it deletes files
  • Assuming it converts formats
2. Which of the following is the correct way to open a CSV file for reading in Python?
easy
A. open('data.csv', 'a')
B. open('data.csv', 'w')
C. open('data.csv', 'r')
D. open('data.csv', 'x')

Solution

  1. Step 1: Understand file modes in Python

    The mode 'r' means open for reading, which is needed to read a CSV file.
  2. Step 2: Check other modes

    'w' is for writing (overwrites), 'a' is for appending, and 'x' is for creating a new file. None are for reading existing files.
  3. Final Answer:

    open('data.csv', 'r') -> Option C
  4. Quick Check:

    Use 'r' mode to read files [OK]
Hint: Use 'r' mode to read files [OK]
Common Mistakes:
  • Using 'w' which overwrites file
  • Using 'a' which appends data
  • Using 'x' which fails if file exists
3. What will be the output of this code snippet?
import csv
with open('data.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['Alice', '30'])

with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    rows = list(reader)
print(rows)
medium
A. ['Name', 'Age', 'Alice', '30']
B. SyntaxError
C. [['Name, Age'], ['Alice, 30']]
D. [['Name', 'Age'], ['Alice', '30']]

Solution

  1. Step 1: Writing rows with csv.writer

    The code writes two rows: header ['Name', 'Age'] and data ['Alice', '30'] as lists.
  2. Step 2: Reading rows with csv.reader

    Reading back returns a list of lists, each inner list is a row split by commas.
  3. Final Answer:

    [['Name', 'Age'], ['Alice', '30']] -> Option D
  4. Quick Check:

    csv.reader returns list of lists [OK]
Hint: csv.reader returns list of lists, not flat list [OK]
Common Mistakes:
  • Expecting a flat list instead of list of lists
  • Thinking rows are single strings
  • Syntax errors from missing newline='' in open
4. Identify the error in this code that reads a CSV file:
import csv
with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
    print(row)
medium
A. csv.reader cannot be used with 'with' statement
B. Indentation error in the for loop body
C. File mode should be 'w' instead of 'r'
D. Missing import statement

Solution

  1. Step 1: Check indentation inside the for loop

    The print statement must be indented inside the for loop to run for each row.
  2. Step 2: Verify other parts

    Import is present, file mode 'r' is correct for reading, and csv.reader works with 'with' statement.
  3. Final Answer:

    Indentation error in the for loop body -> Option B
  4. Quick Check:

    Indent loop body correctly [OK]
Hint: Indent inside loops to avoid errors [OK]
Common Mistakes:
  • Not indenting loop body
  • Changing file mode incorrectly
  • Thinking csv.reader can't be used with 'with'
5. You have a CSV file with columns 'Name', 'Age', and 'City'. You want to read it and create a dictionary where keys are names and values are ages (as integers). Which code snippet correctly does this?
hard
A. import csv with open('data.csv', 'r') as f: reader = csv.DictReader(f) result = {row['Name']: int(row['Age']) for row in reader} print(result)
B. import csv with open('data.csv', 'r') as f: reader = csv.reader(f) result = {row[0]: int(row[1]) for row in reader} print(result)
C. import csv with open('data.csv', 'r') as f: reader = csv.DictReader(f) result = {row['Age']: row['Name'] for row in reader} print(result)
D. import csv with open('data.csv', 'r') as f: reader = csv.reader(f) result = {int(row[1]): row[0] for row in reader} print(result)

Solution

  1. Step 1: Use csv.DictReader to access columns by name

    DictReader reads rows as dictionaries, so we can use keys like 'Name' and 'Age'.
  2. Step 2: Create dictionary with names as keys and ages as integer values

    The comprehension uses row['Name'] as key and converts row['Age'] to int for value.
  3. Final Answer:

    import csv with open('data.csv', 'r') as f: reader = csv.DictReader(f) result = {row['Name']: int(row['Age']) for row in reader} print(result) -> Option A
  4. Quick Check:

    DictReader + dict comprehension with int conversion [OK]
Hint: Use DictReader and convert age to int in comprehension [OK]
Common Mistakes:
  • Using csv.reader without column names
  • Swapping keys and values
  • Not converting age to int