platypush/plugins/csv/__init__.py
def write(self,
              filename: str,
              rows: List[Union[List[Any], Dict[str, Any]]],
              truncate: bool = False,
              delimiter: str = ',',
              quotechar: Optional[str] = '"',
              dialect: str = 'excel'):
        """
        Writes lines to a CSV file.

        :param filename: Path of the CSV file.
        :param rows: Rows to write. It can be a list of lists or a key->value dictionary where the keys match
            the names of the columns. If the rows are dictionaries then a header with the column names will be
            written to the file if not available already, otherwise no header will be written.
        :param truncate: If True then any previous file content will be removed, otherwise the new rows will be
            appended to the file (default: False).
        :param delimiter: Field delimiter (default: ``,``).
        :param quotechar: Quote character (default: ``"``).
        :param dialect: CSV dialect (default: ``excel``).
        """
        filename = self._get_path(filename)
        file_exists = os.path.isfile(filename)
        column_names = []
        csv_args = {
            'delimiter': delimiter,
            'quotechar': quotechar,
            'dialect': dialect,
        }

        if file_exists:
            column_names = self._parse_header(filename, **csv_args)
        elif rows and isinstance(rows[0], dict):
            column_names = rows[0].keys()

        column_name_to_idx = {name: i for i, name in enumerate(column_names)}
        if truncate:
            file_exists = False

        with open(filename, 'w' if truncate else 'a', newline='') as f:
            writer = csv.writer(f, **csv_args)
            if not file_exists and column_names:
                writer.writerow(column_names)

            for row in rows:
                if isinstance(row, dict):
                    flat_row = [None] * len(column_names)
                    for column, value in row.items():
                        assert column in column_name_to_idx, \
                            'No such column available in the CSV file: {}'.format(column)
                        idx = column_name_to_idx[column]
                        flat_row[idx] = value

                    row = flat_row

                writer.writerow(row)

        Writes lines to a CSV file.

        :param filename: Path of the CSV file.
        :param rows: Rows to write. It can be a list of lists or a key->value dictionary where the keys match
            the names of the columns. If the rows are dictionaries then a header with the column names will be
            written to the file if not available already, otherwise no header will be written.
        :param truncate: If True then any previous file content will be removed, otherwise the new rows will be
            appended to the file (default: False).
        :param delimiter: Field delimiter (default: ``,``).
        :param quotechar: Quote character (default: ``"``).
        :param dialect: CSV dialect (default: ``excel``).
        
Writes lines to a CSV file.
Writes lines to a CSV file .
def write(self,
              filename: str,
              rows: List[Union[List[Any], Dict[str, Any]]],
              truncate: bool = False,
              delimiter: str = ',',
              quotechar: Optional[str] = '"',
              dialect: str = 'excel'):
        filename = self._get_path(filename)
        file_exists = os.path.isfile(filename)
        column_names = []
        csv_args = {
            'delimiter': delimiter,
            'quotechar': quotechar,
            'dialect': dialect,
        }
        if file_exists:
            column_names = self._parse_header(filename, **csv_args)
        elif rows and isinstance(rows[0], dict):
            column_names = rows[0].keys()
        column_name_to_idx = {name: i for i, name in enumerate(column_names)}
        if truncate:
            file_exists = False
        with open(filename, 'w' if truncate else 'a', newline='') as f:
            writer = csv.writer(f, **csv_args)
            if not file_exists and column_names:
                writer.writerow(column_names)
            for row in rows:
                if isinstance(row, dict):
                    flat_row = [None] * len(column_names)
                    for column, value in row.items():
                        assert column in column_name_to_idx, \
                            'No such column available in the CSV file: {}'.format(column)
                        idx = column_name_to_idx[column]
                        flat_row[idx] = value
                    row = flat_row
                writer.writerow(row)
def write ( self , filename : str , rows : List [ Union [ List [ Any ] , Dict [ str , Any ] ] ] , truncate : bool = False , delimiter : str = ',' , quotechar : Optional [ str ] = '"' , dialect : str = 'excel' ) : filename = self . _get_path ( filename ) file_exists = os . path . isfile ( filename ) column_names = [ ] csv_args = { 'delimiter' : delimiter , 'quotechar' : quotechar , 'dialect' : dialect , } if file_exists : column_names = self . _parse_header ( filename , ** csv_args ) elif rows and isinstance ( rows [ 0 ] , dict ) : column_names = rows [ 0 ] . keys ( ) column_name_to_idx = { name : i for i , name in enumerate ( column_names ) } if truncate : file_exists = False with open ( filename , 'w' if truncate else 'a' , newline = '' ) as f : writer = csv . writer ( f , ** csv_args ) if not file_exists and column_names : writer . writerow ( column_names ) for row in rows : if isinstance ( row , dict ) : flat_row = [ None ] * len ( column_names ) for column , value in row . items ( ) : assert column in column_name_to_idx , 'No such column available in the CSV file: {}' . format ( column ) idx = column_name_to_idx [ column ] flat_row [ idx ] = value row = flat_row writer . writerow ( row )
Writes lines to a CSV file.
Writes lines to a CSV file .
"""
        Writes lines to a CSV file.

        :param filename: Path of the CSV file.
        :param rows: Rows to write. It can be a list of lists or a key->value dictionary where the keys match
            the names of the columns. If the rows are dictionaries then a header with the column names will be
            written to the file if not available already, otherwise no header will be written.
        :param truncate: If True then any previous file content will be removed, otherwise the new rows will be
            appended to the file (default: False).
        :param delimiter: Field delimiter (default: ``,``).
        :param quotechar: Quote character (default: ``"``).
        :param dialect: CSV dialect (default: ``excel``).
        """
self
filename
str
Path of the CSV file.
Path of the CSV file .
rows
List[Union[List[Any], Dict[str, Any]]]
Rows to write. It can be a list of lists or a key->value dictionary where the keys match
the names of the columns. If the rows are dictionaries then a header with the column names will be
written to the file if not available already, otherwise no header will be written.
Rows to write . It can be a list of lists or a key - > value dictionary where the keys match the names of the columns . If the rows are dictionaries then a header with the column names will be written to the file if not available already otherwise no header will be written .
truncate
bool
If True then any previous file content will be removed, otherwise the new rows will be
appended to the file (default: False).
If True then any previous file content will be removed otherwise the new rows will be appended to the file ( default : False ) .
delimiter
str
Field delimiter (default: ``,``).
Field delimiter ( default : ` ` ` ` ) .
quotechar
Optional[str]
Quote character (default: ``"``).
Quote character ( default : ` ` " ` ` ) .
dialect
str
CSV dialect (default: ``excel``).
CSV dialect ( default : ` ` excel ` ` ) .