def utf_encode(data):
    """ Encode string as UTF-8 """
    if isinstance(data, bytes):
        return data

    try:
        encoded = data.encode('utf-8')
        return encoded
    except ValueError:
        return data