#139 Python for data 6.8 min
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.
Fix one rejected row
Copy the code folder first. In rows.py change the email chen@example to chen@example.com. Before running, predict how many rows will be valid and how many errors python run.py bad will print for that row. Then run python run.py good and python run.py bad to check. This needs Pydantic and its helper packages as in the video's requirements.
A hint
Look at the pattern in models.py: the part after the at sign needs a dot.
Change the allowed countries
In models.py add "DE" to the Literal list so it reads Literal["GB", "IE", "FR", "DE"]. Predict how many errors the row for dina@example.com now has and which one disappears. Then run python run.py bad and compare.
A hint
Dina's row had three errors before; the country error is the one that goes away.
Change the business rule
In models.py change the made-up year rule from 2026 to 2025 (both the number in the if and the message). Predict how many rows are valid and which rows now fail with the year error. Run only python run.py bad; python run.py good will stop with an IndexError because no row is valid. Put the rule back to 2026 afterwards.
A hint
Only one row in the data has a 2025 date, and it also has another problem.
Turn on strict for one row
In run.py strict mode, change rows[0] in the first strict call to a row from rows with a different order, for example rows[1]. Predict whether the error count in 'strict, CSV strings' stays at 4, and why. Run python run.py strict to check.
A hint
Count which fields are not already a string in the row you chose.
These tasks are for you to try on your own machine. Nothing is checked or saved.
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.
