Programming with Python · free preview

The Standard Library in Depth

Parse and write CSV with the csv module, mine text with regular expressions, and build paths safely with pathlib.

1. Overview

Overview

2. Reading CSV Properly

Reading CSV Properly

3. Finding Patterns with re

Finding Patterns with re

4. Building Paths with pathlib

Building Paths with pathlib

5. Sum a CSV Column

Sum a CSV Column

6. Extract Dates with re

Extract Dates with re

7. The collections toolbox

The collections toolbox

8. Summarize CSV rows

CSV rows 36.6, 37.4, and 38.6 follow one header row. Answer as row_count, rounded_total.

9. From CSV text to a total and a date capture

Parse a three-row CSV text, skip the header, sum the reading column, and capture the year groups from the two visit dates. Predict the row count, the total, and the first and last captured year.

Example. From CSV text to a total and a date capture

Problem. Parse a three-row CSV text, skip the header, sum the reading column, and capture the year groups from the two visit dates. Predict the row count, the total, and the first and last captured year.

Correct solution.

python
import csv
import io
import re

text = "patient,visit,reading\nAlice,2026-03-01,36.6\nBob,2026-03-15,38.3\nCarol,2026-03-20,39.7\n"
rows = list(csv.reader(io.StringIO(text)))[1:]
total = sum(float(row[2]) for row in rows)
dates = re.findall(r"(\d{4})-(\d{2})-(\d{2})", text)

print(len(rows), round(total, 1))
print(dates[0], dates[0][0], dates[-1][0])

关键提醒。

Input is three data rows after the header, with readings 36.6, 38.3, and 39.7.

Trace:

  1. list(csv.reader(io.StringIO(text))) returns four rows, and [1:] drops the header, leaving rows for Alice, Bob, and Carol.
  2. float(row[2]) converts each reading string. The sum is 36.6 + 38.3 + 39.7, which is 114.6.
  3. re.findall with three capture groups returns one tuple per date, so dates[0] is ('2026', '03', '01').
  4. dates[0][0] is the first year and dates[-1][0] is the last year.

Expected output:

Output

3 114.6
('2026', '03', '01') 2026 2026

10. Retrieval practice

Given text = "patient,note\nAlice,\"36.6, high\"\n", use csv.DictReader to print the field names and the note value, then use a capturing pattern with re.search to print the year from report = "visit 2026-03-01". Give exact output.

python
import csv
import io
import re

text = "patient,note\nAlice,\"36.6, high\"\n"
report = "visit 2026-03-01"

11. Mastery check

Given the CSV text below, parse the rows, print the number of data rows and the rounded reading total, then print the month captured from the first visit date and the pathlib path reports / 2026-03.csv. Write the code and exact output.

python
import csv
import io
import re
from pathlib import Path

text = "patient,visit,reading\nAlice,2026-03-01,36.6\nBob,2026-03-02,37.4\nCarol,2026-03-03,38.6\n"
report_path = Path("reports") / "2026-03.csv"

Practice this interactively

Free account · instant grading · spaced review that schedules itself.

Start this course for free