Normalize UTF-8 characters of a file

def utf8_normalize(input_filename, comment_char='#', to_upper=False):
    """Normalize UTF-8 characters of a file
    """
    # Prepare the input dictionary file in UTF-8 and NFC
    temp_dict_fd, output_filename = tempfile.mkstemp()

    logging.debug('to_nfc from file {} to file {}'.format(input_filename, output_filename))

    with codecs.open(output_filename, 'w', 'utf-8') as f:
        with codecs.open(input_filename, 'r', 'utf-8') as input_f:
            lines = sorted([unicodedata.normalize(UTF8_NORMALIZATION, l)
                            for l in input_f.readlines()
                            if not l.strip().startswith(comment_char)])
            if to_upper:
                lines = [l.upper() for l in lines]

            f.writelines(lines)

    return output_filename