def encodenonascii(txt):
    """Convert non-ASCII characters to HTML entities"""
    res = []
    for char in txt:
        if ord(char) < 128:
            res.append(char)
        else:
            res.append('&#%i;' % ord(char))
    return ''.join(res)