@pexdoc.pcontracts.contract(<EOL>fname="<STR_LIT>",<EOL>filtered="<STR_LIT>",<EOL>header="<STR_LIT>",<EOL>append=bool,<EOL>)<EOL><INDENT>def write(self, fname=None, filtered=False, header=True, append=False):<DEDENT>
<EOL>write_ex = pexdoc.exh.addex(ValueError, "<STR_LIT>")<EOL>fname = self._fname if fname is None else fname<EOL>data = self.data(filtered=filtered)<EOL>write_ex((len(data) == <NUM_LIT:0>) or ((len(data) == <NUM_LIT:1>) and (len(data[<NUM_LIT:0>]) == <NUM_LIT:0>)))<EOL>if header:<EOL><INDENT>header = [header] if isinstance(header, str) else header<EOL>cfilter = self._gen_col_index(filtered=filtered)<EOL>filtered_header = (<EOL>[self._header[item] for item in cfilter]<EOL>if self._has_header<EOL>else cfilter<EOL>)<EOL>file_header = filtered_header if isinstance(header, bool) else header<EOL><DEDENT>data = [["<STR_LIT>" if item is None else item for item in row] for row in data]<EOL>_write_int(fname, [file_header] + data if header else data, append=append)<EOL>
r"""
        Write (processed) data to a specified comma-separated values (CSV) file.

        :param fname: Name of the comma-separated values file to be
                      written. If None the file from which the data originated
                      is overwritten
        :type  fname: :ref:`FileName`

        :param filtered: Filtering type
        :type  filtered: :ref:`CsvFiltered`

        :param header: If a list, column headers to use in the file. If
                       boolean, flag that indicates whether the input column
                       headers should be written (True) or not (False)
        :type  header: string, list of strings or boolean

        :param append: Flag that indicates whether data is added to an
                       existing file (or a new file is created if it does not
                       exist) (True), or whether data overwrites the file
                       contents (if the file exists) or creates a new file if
                       the file does not exists (False)
        :type  append: boolean

        .. [[[cog cog.out(exobj.get_sphinx_autodoc()) ]]]
        .. Auto-generated exceptions documentation for
        .. putil.pcsv.csv_file.CsvFile.write

        :raises:
         * OSError (File *[fname]* could not be created: *[reason]*)

         * RuntimeError (Argument \`append\` is not valid)

         * RuntimeError (Argument \`filtered\` is not valid)

         * RuntimeError (Argument \`fname\` is not valid)

         * RuntimeError (Argument \`header\` is not valid)

         * RuntimeError (Argument \`no_empty\` is not valid)

         * ValueError (There is no data to save to file)

        .. [[[end]]]