Convert any text file to utf8 encoding.
548566
def to_utf8(path, output_path=None):
    
    if output_path is None:
        basename, ext = os.path.splitext(path)
        output_path = basename + "-UTF8Encode" + ext
    
    text = smartread(path)
    write(text, output_path)
713473
function toUTF8Bytes(input){
    // utf16 -> utf8
    input = toUTF8(input);

    // string -> array of bytes
    var len = input.length;
    var output = new Array(len);

    for (var i = 0; i < len; i++)
      output[i] = input.charCodeAt(i);

    return output;
  }
455976
def _strify(s):
    """"""
    if s is None:
        return None
    if isinstance(s, unicode):
        return s.encode("utf-8")
    return str(s)
709317
def to_unicode(value, encoding=None):
    
    if isinstance(value, text_type):
        return value
    elif isinstance(value, type(None)):
        return text_type(value)

    if not isinstance(value, bytes):
        raise TypeError(
            "Expected bytes, unicode, or None; got %r" % type(value)
        )

    if encoding is not None:
        return value.decode(encoding)
    else:
        try:
            value = value.decode('utf-8')
        except:
            try:
                value = value.decode('gbk')
            except:
                pass

        return value
1113722
def _ensure_unicode(text):
    
    if isinstance(text, six.binary_type):
        return text.decode(sys.getfilesystemencoding(), 'replace')
    else:
        return text
895344
def make_str(value):
    """"""
    if isinstance(value, bytes):
        try:
            return value.decode(sys.getfilesystemencoding())
        except UnicodeError:
            return value.decode('utf-8', 'replace')
    return text_type(value)
1121473
def to_unicode(path, errors="replace"):
    """"""
    if isinstance(path, UNICODE):
        return path
    return path.decode(sys.getfilesystemencoding(), errors)
702550
def ensure_utf8_streams():
        

        def ensure_utf8_stream(stream):
            if not isinstance(stream, io.StringIO):
                stream = getwriter("utf-8")(getattr(stream, "buffer", stream))
                stream.encoding = "utf-8"
            return stream

        sys.stdout, sys.stderr = (ensure_utf8_stream(s)
                                  for s in (sys.stdout, sys.stderr))
1281194
def to_unicode(string):
    """"""
    if not is_unicode(string):
        for codec in CODECS:
            try:
                unic = to_text_string(string, codec)
            except UnicodeError:
                pass
            except TypeError:
                break
            else:
                return unic
    return string
49307
def _unicode_to_native(s):
  """"""
  if six.PY2:
    return s.encode("utf-8") if isinstance(s, unicode) else s
  else:
    return s
770252
def _utf8str(x):
  """"""
  if six.PY3:
    return str(x)
  if isinstance(x, six.binary_type):
    return x
  elif isinstance(x, six.text_type):
    return x.encode('utf-8')
  else:
    return six.binary_type(x)
1058237
def to_unicode(value):
    
    if isinstance(value, ffi.CData):
        return ffi.string(value).decode('utf-8')
    elif isinstance(value, binary_type):
        return value.decode('utf-8')
    elif isinstance(value, text_type):
        return value
    else:
        raise ValueError('Value must be text, bytes, or char[]')
576533
def _normalize_utf8_keys(kwargs):
  """"""
  if any(type(key) is binary_type for key in kwargs.keys()):
    # This is to preserve the original dict type for kwargs.
    dict_type = type(kwargs)
    return dict_type([(text_type(k), v) for k, v in kwargs.items()])
  return kwargs
42779
def to_utf8(string):
    
    if isinstance(string, six.text_type):
        return string.encode('utf8')
    if isinstance(string, six.binary_type):
        return string
    return str(string)
1012539
def readme(filename, encoding='utf8'):
    

    with io.open(filename, encoding=encoding) as source:
        return source.read()
65745
def _native_to_unicode(s):
  """"""
  if six.PY2:
    return s if isinstance(s, unicode) else s.decode("utf-8")
  else:
    return s
265382
def to_utf8(value):
  
  if isinstance(value, unicode):
    return value.encode('utf-8')

  assert isinstance(value, str)
  return value