def set_attributes(data, pos, attributes):
    """Sets the attributes of the html tag at pos in string data to those
    stored in the 'attributes' dictionary. Any existing attributes will be
    overwritten or deleted.
    """
    assert data[pos] == "<"
    endpos = data.find(">", pos)
    assert endpos > pos
    start = data.find(" ", pos, endpos)
    if start < 0:
        start = endpos
    attr_list = ['%s="%s"' % (key, attributes[key]) for key in attributes]
    attr_str = " " + " ".join(attr_list) + " "
    data = data[:start] + attr_str + data[endpos:]
    return data