import string
import unicodedata


def clean_filename(filename: str, replace: str = " ") -> str:
    """Clean filename to valid format which can be used file operations.

    :param filename: name to be cleaned
    :param replace: characters to replace with underscore, defaults to " "
    :return: valid filename
    """
    valid_characters = "-_.()" + string.ascii_letters + string.digits

    for char in replace:
        filename = filename.replace(char, "_")

    clean = unicodedata.normalize("NFKD", filename)
    clean = clean.encode("ASCII", "ignore").decode()
    clean = "".join(char for char in filename if char in valid_characters)

    return clean