Checking for valid and invalid csv headers
This commit is contained in:
		
							parent
							
								
									20d0f163e6
								
							
						
					
					
						commit
						6eebd6a095
					
				@ -7,9 +7,16 @@ def get_csv_data(file):
 | 
			
		||||
    reader = csv.DictReader(csv_file, delimiter='|')
 | 
			
		||||
 | 
			
		||||
    # Validate CSV headers based on config
 | 
			
		||||
    required_headers = set(item_attributes.keys())
 | 
			
		||||
    required_headers = set(item_attributes.keys())  # Get required headers as per configuration
 | 
			
		||||
    csv_headers = set(reader.fieldnames)    # Get headers present in the csv file
 | 
			
		||||
 | 
			
		||||
    # Check if the required headers exist
 | 
			
		||||
    if not required_headers.issubset(reader.fieldnames):
 | 
			
		||||
        raise ValueError(f"CSV file must include headers: {', '.join(required_headers)}.")
 | 
			
		||||
    
 | 
			
		||||
    # Check for invalid headers
 | 
			
		||||
    if extra_headers := csv_headers - valid_headers:
 | 
			
		||||
        raise ValueError(f"Unexpected headers found: {', '.join(extra_headers)}")
 | 
			
		||||
 | 
			
		||||
    # Extract data dynamically based on config
 | 
			
		||||
    return [{attrib: row[attrib] for attrib in item_attributes} for row in reader]
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user