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
Files
- models.py (34 lines)
- rows.py (17 lines)
- run.py (38 lines)
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
- https://pydantic.dev/docs/validation/latest/concepts/fields/
- https://pydantic.dev/docs/validation/latest/concepts/models/
- https://pydantic.dev/docs/validation/latest/concepts/strict_mode/
- https://pydantic.dev/docs/validation/latest/concepts/validators/
- https://pydantic.dev/docs/validation/latest/errors/validation_errors/
- https://pypi.org/pypi/annotated-types/json
- https://pypi.org/pypi/pydantic-core/json
- https://pypi.org/pypi/pydantic/json
- https://pypi.org/pypi/typing-extensions/json
- https://pypi.org/pypi/typing-inspection/json
Learning IT by Example.