import os
import logging
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
def normalize_env_vars(env_vars):
"""
Normalizes environment variables by converting keys to lowercase and
replacing spaces with underscores. Handles potential errors during
processing.
"""
normalized_vars = {}
for key, value in env_vars.items():
try:
normalized_key = key.lower().replace(" ", "_") # Normalize key
normalized_vars[normalized_key] = value
except Exception as e:
logging.error(f"Error normalizing key '{key}': {e}")
# Optionally, continue to the next variable or raise the exception
continue
return normalized_vars
if __name__ == '__main__':
# Example usage (replace with your actual environment variables)
env_variables = os.environ.copy() # Copy environment variables
normalized_variables = normalize_env_vars(env_variables)
print(normalized_variables)
Add your comment