def ToCsv(self, columns_order=None, order_by=(), separator="<STR_LIT:U+002C>"):
csv_buffer = six.StringIO()<EOL>writer = csv.writer(csv_buffer, delimiter=separator)<EOL>if columns_order is None:<EOL><INDENT>columns_order = [col["<STR_LIT:id>"] for col in self.__columns]<EOL><DEDENT>col_dict = dict([(col["<STR_LIT:id>"], col) for col in self.__columns])<EOL>def ensure_str(s):<EOL><INDENT>"<STR_LIT>"<EOL>if isinstance(s, str):<EOL><INDENT>return s<EOL><DEDENT>return s.encode("<STR_LIT:utf-8>")<EOL><DEDENT>writer.writerow([ensure_str(col_dict[col]["<STR_LIT:label>"])<EOL>for col in columns_order])<EOL>for row, unused_cp in self._PreparedData(order_by):<EOL><INDENT>cells_list = []<EOL>for col in columns_order:<EOL><INDENT>value = "<STR_LIT>"<EOL>if col in row and row[col] is not None:<EOL><INDENT>value = self.CoerceValue(row[col], col_dict[col]["<STR_LIT:type>"])<EOL><DEDENT>if isinstance(value, tuple):<EOL><INDENT>if col_dict[col]["<STR_LIT:type>"] in ["<STR_LIT:date>", "<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>cells_list.append(ensure_str(self.ToString(value[<NUM_LIT:1>])))<EOL><DEDENT>else:<EOL><INDENT>cells_list.append(ensure_str(self.ToString(value[<NUM_LIT:0>])))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>cells_list.append(ensure_str(self.ToString(value)))<EOL><DEDENT><DEDENT>writer.writerow(cells_list)<EOL><DEDENT>return csv_buffer.getvalue()<EOL>
Writes the data table as a CSV string.

        Output is encoded in UTF-8 because the Python "csv" module can't handle
        Unicode properly according to its documentation.

        Args:
          columns_order: Optional. Specifies the order of columns in the
                         output table. Specify a list of all column IDs in the order
                         in which you want the table created.
                         Note that you must list all column IDs in this parameter,
                         if you use it.
          order_by: Optional. Specifies the name of the column(s) to sort by.
                    Passed as is to _PreparedData.
          separator: Optional. The separator to use between the values.

        Returns:
          A CSV string representing the table.
          Example result:
           'a','b','c'
           1,'z',2
           3,'w',''

        Raises:
          DataTableException: The data does not match the type.