def to_unicode(value): try: value = six.text_type(force_text(value)) except (UnicodeEncodeError, UnicodeDecodeError): value = '(Error decoding value)' except Exception: # in some cases we get a different exception try: value = six.binary_type(repr(type(value))) except Exception: value = '(Error decoding value)' return value
def to_string(value): try: return six.binary_type(value.decode('utf-8').encode('utf-8')) except: return to_unicode(value).encode('utf-8')