flask_crud_app/process_csv.py

29 lines
1009 B
Python

import csv
from io import TextIOWrapper
def get_csv_data(file):
"""
Processes the uploaded CSV file and returns a list of asset dictionaries.
Raises an exception if the file is invalid or data is incorrect.
"""
# Open and process the CSV file
csv_file = TextIOWrapper(file, encoding='utf-8')
reader = csv.DictReader(csv_file, delimiter='|')
# Validate CSV headers
required_headers = ['assettag', 'hostname', 'warrantyfrom', 'status', 'staffnum']
if not all(header in reader.fieldnames for header in required_headers):
raise ValueError("CSV file must include headers: assettag, hostname, warrantyfrom, status, staffnum.")
# Extract data from the CSV file
assets = []
for row in reader:
assets.append({
'assettag': row['assettag'],
'hostname': row['hostname'],
'warrantyfrom': row['warrantyfrom'],
'status': row['status'],
'staffnum': row['staffnum']
})
return assets