28 lines
728 B
Python
28 lines
728 B
Python
|
|
import csv
|
||
|
|
import sys
|
||
|
|
|
||
|
|
# Define the expected number of columns
|
||
|
|
EXPECTED_COLUMNS = 5
|
||
|
|
|
||
|
|
# Input CSV file
|
||
|
|
if len(sys.argv) != 2:
|
||
|
|
print("Usage: python validate_csv.py <csv_file>")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
CSV_FILE = sys.argv[1]
|
||
|
|
|
||
|
|
# Check if the file exists
|
||
|
|
try:
|
||
|
|
with open(CSV_FILE, 'r') as file:
|
||
|
|
reader = csv.reader(file)
|
||
|
|
for line_number, row in enumerate(reader, start=1):
|
||
|
|
if len(row) != EXPECTED_COLUMNS:
|
||
|
|
print(f"Error: Line {line_number} has {len(row)} columns (expected {EXPECTED_COLUMNS}).")
|
||
|
|
sys.exit(1)
|
||
|
|
except FileNotFoundError:
|
||
|
|
print(f"Error: File '{CSV_FILE}' not found.")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
print(f"Validation passed: All rows have {EXPECTED_COLUMNS} columns.")
|
||
|
|
|