1. import os
  2. import logging
  3. logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
  4. def normalize_env_vars(env_vars):
  5. """
  6. Normalizes environment variables by converting keys to lowercase and
  7. replacing spaces with underscores. Handles potential errors during
  8. processing.
  9. """
  10. normalized_vars = {}
  11. for key, value in env_vars.items():
  12. try:
  13. normalized_key = key.lower().replace(" ", "_") # Normalize key
  14. normalized_vars[normalized_key] = value
  15. except Exception as e:
  16. logging.error(f"Error normalizing key '{key}': {e}")
  17. # Optionally, continue to the next variable or raise the exception
  18. continue
  19. return normalized_vars
  20. if __name__ == '__main__':
  21. # Example usage (replace with your actual environment variables)
  22. env_variables = os.environ.copy() # Copy environment variables
  23. normalized_variables = normalize_env_vars(env_variables)
  24. print(normalized_variables)

Add your comment