<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def ToCsv(self, columns_order=None, order_by=(), separator=","):
 """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. """


    csv_buffer = six.StringIO()
    writer = csv.writer(csv_buffer, delimiter=separator)

    if columns_order is None:
      columns_order = [col["id"] for col in self.__columns]
    col_dict = dict([(col["id"], col) for col in self.__columns])

    def ensure_str(s):
      "Compatibility function. Ensures using of str rather than unicode."
      if isinstance(s, str):
        return s
      return s.encode("utf-8")

    writer.writerow([ensure_str(col_dict[col]["label"])
                     for col in columns_order])

    # We now go over the data and add each row
    for row, unused_cp in self._PreparedData(order_by):
      cells_list = []
      # We add all the elements of this row by their order
      for col in columns_order:
        value = ""
        if col in row and row[col] is not None:
          value = self.CoerceValue(row[col], col_dict[col]["type"])
        if isinstance(value, tuple):
          # We have a formatted value. Using it only for date/time types.
          if col_dict[col]["type"] in ["date", "datetime", "timeofday"]:
            cells_list.append(ensure_str(self.ToString(value[1])))
          else:
            cells_list.append(ensure_str(self.ToString(value[0])))
        else:
          cells_list.append(ensure_str(self.ToString(value)))
      writer.writerow(cells_list)
    return csv_buffer.getvalue()