def clean_id(id):
    """
    Return id with initial and final whitespace removed, and
    with any internal whitespace sequences replaced by a single
    blank.  Also, all nonprintable characters are removed.
    """

    id = id.strip()
    new_id = ""
    for c in id:
        if c.isspace():
            c = " "
        if (c != " " or (len(new_id)>0 and new_id[-1] != " ")) \
           and c.isprintable():
            new_id += c
    return new_id