<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def urlencode_unicode(data, doseq=0):
 """urllib.urlencode can't handle unicode, this is a hack to fix it."""

    data_iter = None
    if isdict(data):
        data_iter = list(data.items())
    elif islist(data):
        data_iter = data

    if data_iter:
        for i, (key, value) in enumerate(data_iter):
            if isinstance(value, text_type):
                # try to convert to str
                try:
                    safe_val = str(value)
                except UnicodeEncodeError:
                    # try to encode as utf-8
                    # if an exception is raised here then idk what to do
                    safe_val = value.encode('utf-8')
                finally:
                    if isdict(data):
                        data[key] = safe_val
                    else:
                        data[i] = (key, safe_val)

    return urlencode(data, doseq=doseq)