The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a CSV validator tool

Here is a simple CSV validator tool written in Python. This script checks for:

  • File existence and format (.csv)

  • Consistent number of columns per row

  • Presence of a header

  • Optional checks for empty values or custom rules

You can copy and run this as a script or convert it into a web tool or CLI tool if needed.

python
import csv import os def validate_csv(file_path, required_columns=None, allow_empty=False): if not os.path.isfile(file_path): return "Error: File does not exist." if not file_path.lower().endswith('.csv'): return "Error: File is not a CSV." with open(file_path, newline='', encoding='utf-8') as csvfile: reader = csv.reader(csvfile) try: header = next(reader) except StopIteration: return "Error: CSV file is empty." if required_columns: missing_columns = [col for col in required_columns if col not in header] if missing_columns: return f"Error: Missing required columns: {missing_columns}" column_count = len(header) row_number = 1 for row in reader: row_number += 1 if len(row) != column_count: return f"Error: Row {row_number} has {len(row)} columns; expected {column_count}." if not allow_empty and any(cell.strip() == '' for cell in row): return f"Error: Row {row_number} contains empty cells." return "CSV validation passed successfully." # Example usage: if __name__ == "__main__": path_to_csv = "example.csv" required = ["Name", "Email", "Age"] # Customize as needed result = validate_csv(path_to_csv, required_columns=required, allow_empty=False) print(result)

Features:

  • Validates if file is .csv.

  • Confirms header exists.

  • Ensures each row has same number of columns.

  • Optionally checks for required headers and disallows empty values.

Let me know if you want this tool adapted for web (HTML/JS), command-line interface, or GUI version.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About