nothing / aicg /validatecsv.py
noznarb's picture
Added aicg
9963a74
raw
history blame
1.28 kB
# Import the csv and os modules
import csv
import os
# Define the directory where the csv files are located
directory = "."
# Define a function to validate a csv file
def validate_csv(file):
# Open the file in read mode
with open(file, "r", encoding="utf-8") as f:
# Create a csv reader object
reader = csv.reader(f)
# Get the header row
header = next(reader)
# Check if the header has the expected number of columns
if len(header) != 4:
# Return False if not
return False
# Loop through the rest of the rows
for row in reader:
# Check if each row has the same number of columns as the header
if len(row) != len(header):
# Return False if not
return False
# Return True if no errors are found
return True
# Loop through each file in the directory
for file in os.listdir(directory):
# Check if the file is a csv file
if file.endswith(".csv"):
# Validate the csv file and print the result
print(f"Validating {file}...")
result = validate_csv(os.path.join(directory, file))
if result:
print(f"{file} is valid.")
else:
print(f"{file} is invalid.")