Validate CSV columns and detect duplicate rows with Python
Python
Check required CSV columns and identify exact duplicate rows before an import.
Inspect modest comma-separated CSV inputs without changing them. This standard-library function checks the header and record width, then reports repeated rows against their first occurrence.
Before you start
Python 3.12; no external dependencies.
Decoded CSV text using comma separators.
Pass required column names as a list of strings.
Build and check
Save the example code as recipe.py.
Import inspect_csv and pass the CSV text and required column names.
Inspect rows and duplicates in the returned dictionary.
Handle ValueError and csv.Error before proceeding with an import.
Limits to understand
Compares complete rows exactly, including whitespace and letter case.
Does not validate data types or identifier uniqueness.
Accepts decoded text, not a file path; encoding is the caller's responsibility.
Record numbers count CSV records, not physical lines.
Memory grows with distinct rows; use modest inputs.
Header-only input is valid. Additional columns are allowed.
Run it
from recipe import inspect_csv
result = inspect_csv("id,name\n1,Jose\n1,Jose\n", ["id", "name"])
print(result)
Code license: MIT. Keep the license notice with your copy.
Example version 0.1.0 · Documentation reviewed 2026-10-13.
Seven unittest methods passed in an isolated Python 3.12 Docker container with networking disabled. Covers the listed fixtures; does not establish performance for large files or support for every CSV dialect.
Related browser tools
Inspect related data locally in your browser. These tools have their own input rules and limits; they do not execute this recipe.