learning-it by example

Back to the video

Validate Data with Pydantic: Catch Bad Rows

Six made-up CSV rows through Pydantic: 3 valid, 3 rejected, with 6 named errors in one pass.

Video #139 ยท Python for data

Steps in the video

  1. 0:00 Six rows in, three rejected, six reasons why
  2. 0:26 The idea
  3. 0:56 Setup
  4. 1:20 The made-up rows
  5. 1:51 The model
  6. 2:18 A rule of your own
  7. 2:42 Split good from bad
  8. 3:00 The run
  9. 4:21 Strict mode
  10. 5:10 What this does not show
  11. 5:32 Mistakes to avoid
  12. 5:58 My take and next steps
  13. 6:20 Recap

Files

  • models.py (34 lines)
  • rows.py (17 lines)
  • run.py (38 lines)

Download the code (zip)

Tested with

Python 3.12.15 | annotated-types==0.8.0, pydantic==2.14.1, pydantic-core==2.50.1, typing-extensions==4.16.0, typing-inspection==0.4.4

What this does not show

Six made-up rows and a rough email pattern; it shows how errors are reported, not how Pydantic handles a real file, JSON input or speed.

Sources

Learning IT by Example.