Skip to content
All courses
PythonIntermediate

Working with Data in Python

Clean, read, count and report on everyday data using only the Python standard library: text, CSV, JSON, dates and tidy text reports.

Recommended first: Intermediate Python. This course builds on it.

Free to browse. You sign in at the first lesson so your progress is saved.

What you will be able to do

  • Clean messy text with string methods and simple regular expressions
  • Read and write CSV and JSON with the csv and json modules
  • Count, group, sort and average records with collections and statistics
  • Parse, format and compare dates with datetime and timedelta
  • Turn raw CSV text into a neatly aligned spending report

Syllabus

6 modules and 21 lessons. Each lesson is a short read followed by an exercise with tests.

Module 1

Cleaning text

Tidy messy text with string methods and simple regular expressions.4 lessons, about 49 min
  1. 1Trimming spaces and fixing caseRemove stray whitespace and make capital letters consistent.10 min
  2. 2Splitting and joining textBreak a line of text into a list, then glue a list back into text.12 min
  3. 3Replacing and testing textSwap out characters and check what a string starts with or contains.12 min
  4. 4Finding patterns with reUse regular expressions to pull out and tidy text that follows a pattern.15 min
Module 2

Reading and writing CSV

Read rows from CSV text, handle quoted fields and write CSV of your own.4 lessons, about 52 min
  1. 5Reading CSV rowsUse csv.reader and io.StringIO to loop over the rows of a CSV.12 min
  2. 6Rows as dictionariesUse csv.DictReader to look up values by column name.12 min
  3. 7Quoted fields and missing valuesHandle commas inside values and blank cells without crashing.14 min
  4. 8Writing CSV textProduce CSV with csv.writer and csv.DictWriter.14 min
Module 3

Working with JSON

Read nested JSON, write it back out neatly and cope with bad input.3 lessons, about 38 min
  1. 9Reading JSONTurn JSON text into Python dictionaries and lists with json.loads.12 min
  2. 10Writing JSONTurn Python data into readable JSON text with json.dumps.12 min
  3. 11Handling bad JSONCatch JSONDecodeError and use get() for keys that might be missing.14 min
Module 4

Counting and grouping

Count things, group records, sort them and summarise them with averages.4 lessons, about 52 min
  1. 12Counting with CounterTally how often each value appears and find the most common.12 min
  2. 13Grouping with defaultdictCollect records into groups by a shared value.14 min
  3. 14Sorting records by a keySort dictionaries by any field, in either direction, with tie-breaks.14 min
  4. 15Averages with statisticsSummarise numbers with mean, median and friends.12 min
Module 5

Dates and times

Parse dates from text, format them for people and work out gaps between them.3 lessons, about 40 min
  1. 16Turning text into datesCreate date objects and parse UK-style date strings with strptime.14 min
  2. 17Formatting dates for peopleTurn dates into friendly text with strftime and f-strings.12 min
  3. 18Differences with timedeltaSubtract dates, add days and work out how overdue something is.14 min
Module 6

Project: a spending report

Combine everything so far to turn raw bank CSV into a clear monthly report.3 lessons, about 57 min
  1. 19Lining up columnsUse f-string widths and alignment to print tidy tables.14 min
  2. 20Totals by monthParse dates from CSV and add up amounts for each month.18 min
  3. 21Building the full reportCombine grouping, sorting, dates and alignment into one finished report.25 min