Lesson 3.1

Reading and writing CSV and JSON

Reading a CSV as dictionaries rather than positional rows makes downstream code refer to column names, so an inserted column does not silently shift every field.

16mBeginner21.2k students

Overview

The two formats you will meet every week

Reading a CSV as dictionaries rather than positional rows makes downstream code refer to column names, so an inserted column does not silently shift every field.

Encoding is where real CSVs go wrong. Files exported from spreadsheets frequently are not UTF-8, and the resulting decode error is easier to diagnose than the mojibake you get by ignoring it.

JSON handles nested structures but only knows a few types. Dates and decimals need an explicit conversion, and floats will not round-trip a currency amount exactly.

In this lesson you will:

  • Read a CSV into dictionaries
  • Handle encodings and delimiters
  • Serialise data to JSON safely

Resources

Previous Lesson
Next Lesson
Reading and writing CSV and JSON — Python Foundations for Data Work — Vertex