def transformcsv(csv_filepath, output_folder_path): # Covert CSV to DictReader reader = csv_to_dict_reader(csv_filepath) # Check for valid fields result, report = validate_fields(reader) # If field validation failed, do not proceed if result == False: for item in report: print item return report # Setup output CSV files: One for valid rows, one for invalid rows fields = reader.fieldnames if not os.path.exists(output_folder_path): os.makedirs(output_folder_path) invalid_writer = new_csv_file(output_folder_path, "invalid-rows", fields) valid_writer = new_csv_file(output_folder_path, "valid-rows", fields) # Begin looping through rows final_report = list() for index, row in enumerate(reader): row_result, row_report = validate_row(index, row, fields) if row_result == False: # Row was not valid. Write to invalid CSV file. invalid_writer.writerow(row) else: # Row was valid. Write to valid CSV file. valid_writer.writerow(row) # Create XML for this row. save_metadata method automatically validates the saved file. invalid_xml_path = os.path.join(output_folder_path, "invalid-xml") if not os.path.exists(invalid_xml_path): os.makedirs(invalid_xml_path) xml_is_valid, xml_filepath = save_metadata(transform_valid_row(row), invalid_xml_path, index) # Move the XML to the "valid" folder if it was... if xml_is_valid: move_valid_xml(xml_filepath) # Either way, append the report items to the final report for item in row_report: final_report.append(item) # Print the report - No Warnings for item in final_report: if not item.startswith("WARNING"): print item return final_report
def transformcsv(csv_filepath, output_folder_path): # Covert CSV to DictReader reader = csv_to_dict_reader(csv_filepath) # Check for valid fields result, report = validate_fields(reader) # If field validation failed, do not proceed if result == False: for item in report: print item return report # Setup output CSV files: One for valid rows, one for invalid rows fields = reader.fieldnames if not os.path.exists(output_folder_path): os.makedirs(output_folder_path) invalid_writer = new_csv_file(output_folder_path, 'invalid-rows', fields) valid_writer = new_csv_file(output_folder_path, 'valid-rows', fields) # Begin looping through rows final_report = list() for index, row in enumerate(reader): row_result, row_report = validate_row(index, row, fields) if row_result == False: # Row was not valid. Write to invalid CSV file. invalid_writer.writerow(row) else: # Row was valid. Write to valid CSV file. valid_writer.writerow(row) # Create XML for this row. save_metadata method automatically validates the saved file. invalid_xml_path = os.path.join(output_folder_path, 'invalid-xml') if not os.path.exists(invalid_xml_path): os.makedirs(invalid_xml_path) xml_is_valid, xml_filepath = save_metadata( transform_valid_row(row), invalid_xml_path, index) # Move the XML to the "valid" folder if it was... if xml_is_valid: move_valid_xml(xml_filepath) # Either way, append the report items to the final report for item in row_report: final_report.append(item) # Print the report - No Warnings for item in final_report: if not item.startswith('WARNING'): print item return final_report
def validatecsv(filepath): reader = csv_to_dict_reader(filepath) return validate(reader)