def tidy_xml(filename):
    '''
    read in file, screen out unsafe unicode characters, write back file in utf-8

    :param filename: str
    :returns: False if unable to read from file
    '''
    if not os.path.isfile(filename):
        raise ValueError('file does not exist')

    # try first utf-8 then iso. This is ugly, but the files in
    # question that are problematic do not declare unicode type
    data = None
    for ftype in ['utf-8', 'iso8859-1']:
        fhand = None
        try:
            fhand = codecs.open(filename, 'r', ftype)
            data = fhand.read()
            break
        except __HOLE__:
            continue
        finally:
            if fhand is not None:
                fhand.close()

    if data is None:
        return False

    for match in _SAFE_XML_REGEX.finditer(data):
        data = data[:match.start()] + '?' + data[match.end():]

    with open(filename, 'w') as fhand:
        fhand.write(data)
    return True
dataset/ETHPy150Open ros/catkin/python/catkin/tidy_xml.py/tidy_xml