Write a unicode string to a file (as utf-8).
353261
def write(u, path):
    
    print("writing to: %s" % path)
    # This function implementation was chosen to be compatible across Python 2/3.
    f = open(path, "wb")
    try:
        b = u.encode(FILE_ENCODING)
        f.write(b)
    finally:
        f.close()
217216
def write(self, text, encoding="utf-8"):
        
        logger.info("Writing to %s" % self)
        with codecs.open(self.path, 'w', encoding) as fout:
            fout.write(text)
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)
1014929
def write_utf(self, s):
        """"""
        utfstr = s.encode('utf-8')
        length = len(utfstr)
        if length > 64:
            raise NamePartTooLongException
        self.write_byte(length)
        self.write_string(utfstr, length)
1069099
def Write(self, string):
    
    try:
      # Note that encode() will first convert string into a Unicode string
      # if necessary.
      encoded_string = codecs.encode(string, self._encoding, self._errors)
    except UnicodeEncodeError:
      if self._errors == 'strict':
        logger.error(
            'Unable to properly write output due to encoding error. '
            'Switching to error tolerant encoding which can result in '
            'non Basic Latin (C0) characters to be replaced with "?" or '
            '"\\ufffd".')
        self._errors = 'replace'

      encoded_string = codecs.encode(string, self._encoding, self._errors)

    self._file_object.write(encoded_string)
354376
void writeTo(DataOutputStream out) throws IOException {

    // We do the UTF8 conversion ourselves instead of relying on DataOutput
    // to ensure we strictly follow UTF-8 standard, as well as better performance,
    // and save the code to count the UTF-8 bytes (we need that to calculate
    // the total length.
    int length = size() - 4;

    out.writeInt(length);
    out.writeShort(codecNameUTF8.length);
    out.write(codecNameUTF8);
    if (codecNameUTF8.length == 0) {
      out.writeLong(crc32Value);
    }
    out.write(storedData.getData(), 0, storedData.getLength());
  }
1063500
async def dump_unicode(writer, elem):
    
    await dump_uvarint(writer, len(elem))
    await writer.awrite(bytes(elem, 'utf8'))
1145017
def to_unicode(string):
    
    if isinstance(string, six.binary_type):
        return string.decode('utf8')
    if isinstance(string, six.text_type):
        return string
    if six.PY2:
        return unicode(string)
    return str(string)
887374
function (file, content, charset, callback) {
    try {
        if (!file) {
            return;
        }
        charset = (charset || 'utf-8').toLowerCase();
        if (charset !== 'utf-8') {
            content = require('iconv-lite').encode(content + '\r\n', charset);
        }
        callback.call(this, file, content);
    } catch (ex) {
        throw util.format('cant write file [%s]%s for %s', charset, file, ex);
    }
}
462497
def encode_string(self, value):
        """"""
        if not isinstance(value, str): return value
        try:
            return unicode(value, 'utf-8')
        except: # really, this should throw an exception.
                # in the interest of not breaking current
                # systems, however:
            arr = []
            for ch in value:
                arr.append(unichr(ord(ch)))
            return u"".join(arr)
724973
def bytes_to_string(raw):
    """"""
    ret = bytes()
    for byte in raw:
        if byte == 0x00:
            return ret.decode("utf-8")
        ret += bytes([byte])
    return ret.decode("utf-8")
236817
def _WritesString(self, content):
    
    content_bytes = codecs.encode(content, 'utf-8')
    self._sample_file.write(content_bytes)
1015682
def safe_unicode(string):
    """"""
    if not PY3:
        uni = string.replace(u'\u2019', "'")
        return uni.encode('utf-8')
        
    return string
668452
def write(s, path, encoding="utf-8"):
    
    is_gzip = is_gzip_file(path)

    with open(path, "wb") as f:
        if is_gzip:
            f.write(zlib.compress(s.encode(encoding)))
        else:
            f.write(s.encode(encoding))
584144
def bytes_to_str(s, encoding='utf-8'):
    """"""
    if six.PY3 and isinstance(s, bytes):
        return s.decode(encoding)
    return s
1079531
def write(self, filename, encoding='utf-8'):
        
        with io.open(str(filename), 'w', encoding=encoding) as fp:
            for entry in self:
                fp.write(entry.__unicode__())
                fp.write('\n\n')
394301
public void writeUtf8(CharSequence string) {
    for (int i = 0, len = string.length(); i < len; i++) {
      char ch = string.charAt(i);
      if (ch < 0x80) { // 7-bit ASCII character
        writeByte(ch);
        // This could be an ASCII run, or possibly entirely ASCII
        while (i < len - 1) {
          ch = string.charAt(i + 1);
          if (ch >= 0x80) break;
          i++;
          writeByte(ch); // another 7-bit ASCII character
        }
      } else if (ch < 0x800) { // 11-bit character
        writeByte(0xc0 | (ch >> 6));
        writeByte(0x80 | (ch & 0x3f));
      } else if (ch < 0xd800 || ch > 0xdfff) { // 16-bit character
        writeByte(0xe0 | (ch >> 12));
        writeByte(0x80 | ((ch >> 6) & 0x3f));
        writeByte(0x80 | (ch & 0x3f));
      } else { // Possibly a 21-bit character
        if (!Character.isHighSurrogate(ch)) { // Malformed or not UTF-8
          writeByte('?');
          continue;
        }
        if (i == len - 1) { // Truncated or not UTF-8
          writeByte('?');
          break;
        }
        char low = string.charAt(++i);
        if (!Character.isLowSurrogate(low)) { // Malformed or not UTF-8
          writeByte('?');
          writeByte(Character.isHighSurrogate(low) ? '?' : low);
          continue;
        }
        // Write the 21-bit character using 4 bytes
        // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G2630
        int codePoint = Character.toCodePoint(ch, low);
        writeByte(0xf0 | (codePoint >> 18));
        writeByte(0x80 | ((codePoint >> 12) & 0x3f));
        writeByte(0x80 | ((codePoint >> 6) & 0x3f));
        writeByte(0x80 | (codePoint & 0x3f));
      }
    }
  }