def sanitize(s,
             normalize_whitespace=True,
             normalize_unicode=True,
             form='NFKC',
             enforce_encoding=True,
             encoding='utf-8'):
    """Normalize a string

    Args:
        s (unicode string): input unicode string
        normalize_whitespace (bool): if True, normalize all whitespace to single spaces (including newlines),
                                     strip whitespace at start/end
        normalize_unicode (bool): if True, normalize unicode form to 'form'
        form (str): unicode form
        enforce_encoding (bool): if True, encode string to target encoding and re-decode, ignoring errors
                                 and stripping all characters not part of the encoding
        encoding (str): target encoding for the above

    Returns:
        str: unicode output string
    """

    if enforce_encoding:
        s = s.encode(encoding, errors='ignore').decode(encoding, errors='ignore')

    if normalize_unicode:
        s = unicodedata.normalize(form, s)

    if normalize_whitespace:
        s = re.sub(r'\s+', ' ', s).strip()

    return s
def sanitize ( s , normalize_whitespace = True , normalize_unicode = True , form = 'NFKC' , enforce_encoding = True , encoding = 'utf-8' ) : if enforce_encoding : s = s . encode ( encoding , errors = 'ignore' ) . decode ( encoding , errors = 'ignore' ) if normalize_unicode : s = unicodedata . normalize ( form , s ) if normalize_whitespace : s = re . sub ( r'\s+' , ' ' , s ) . strip ( ) return s
Normalize a string

    Args:
        s (unicode string): input unicode string
        normalize_whitespace (bool): if True, normalize all whitespace to single spaces (including newlines),
                                     strip whitespace at start/end
        normalize_unicode (bool): if True, normalize unicode form to 'form'
        form (str): unicode form
        enforce_encoding (bool): if True, encode string to target encoding and re-decode, ignoring errors
                                 and stripping all characters not part of the encoding
        encoding (str): target encoding for the above

    Returns:
        str: unicode output string