How can I validate a CSV column against a set of allowed values?
Quick answer
Read the CSV with csv.DictReader, define the allowed strings in a set, and compare each field value directly against that set. A direct comparison is case-sensitive and does not trim whitespace, so open and Open are unexpected if only open is allowed. Report the data-record number and original value; if you choose to normalize for matching, keep the original value for the report.
The question, the details, the next step.
A practical checklist
Confirm the CSV header name matches the key used in the script.
Write the allowed strings explicitly and confirm them with the rule owner.
Decide whether comparison is exact or explicitly normalized.
Keep the original field value for reporting, even if matching uses a normalized copy.
Label row numbers as data-record numbers or spreadsheet row numbers.
Review unexpected values before making edits to the source file.
A few more useful answers
Does a direct set comparison ignore capitalization or surrounding spaces?
No. It compares the strings exactly, so Open and open differ from open. [S1]
Does this validation decide which values should be allowed?
No. The allowed set and whether to normalize values are application choices; confirm them with the relevant data owner. [S1]
Will the example change the CSV?
No. It reads the file and reports values that are not in the allowed set; it does not write changes.
Sources
References used for this guide.
- [S1] Python 3.14 csv module: reading rows as dictionaries docs.python.org · Checked
