zut.csv.CsvWriter

class zut.csv.CsvWriter(file: str | os.PathLike | IO[str] | None = None, headers: Sequence[str] | HeaderFormatters | Literal[False] | None = None, *, append=False, delay=False, archivate: bool | str | os.PathLike | None = None, accept_additional: bool | Literal['warn', 'discard'] | None = None, accept_missing: bool | Literal['warn'] | None = None, fmt: CsvFormat | str | None = None, encoding: str | None = None, delimiter: str | Literal['locale'] | None = None, decimal_separator: str | Literal['locale'] | None = None, nullval: str | None = None, quotechar: str | None = None, newline: str | None = None, lists: Literal['pg', 'json', 'visual'] | None = None, dicts: Literal['json', 'visual'] | None = None, enums: Literal['value', 'name'] | None = None, no_microsecond: bool | None = None, no_scientific_notation: bool | None = None, tz: tzinfo | str | Literal['local', 'utc'] | None = None)

Bases: object

Write CSV files with flexible header ordering and format options.

__init__(file: str | os.PathLike | IO[str] | None = None, headers: Sequence[str] | HeaderFormatters | Literal[False] | None = None, *, append=False, delay=False, archivate: bool | str | os.PathLike | None = None, accept_additional: bool | Literal['warn', 'discard'] | None = None, accept_missing: bool | Literal['warn'] | None = None, fmt: CsvFormat | str | None = None, encoding: str | None = None, delimiter: str | Literal['locale'] | None = None, decimal_separator: str | Literal['locale'] | None = None, nullval: str | None = None, quotechar: str | None = None, newline: str | None = None, lists: Literal['pg', 'json', 'visual'] | None = None, dicts: Literal['json', 'visual'] | None = None, enums: Literal['value', 'name'] | None = None, no_microsecond: bool | None = None, no_scientific_notation: bool | None = None, tz: tzinfo | str | Literal['local', 'utc'] | None = None)

Write CSV files with flexible header ordering and format options.

Parameters:
  • accept_additional

    Behaviour when rows being written have additional values compared to the file headers:

    • True: append additional values to columns without a header name.

    • ”warn” (default): append additional values to columns without a header name, log a warning.

    • ”discard”: discard additional values.

    • False: raise an error.

  • accept_missing

    Behaviour when rows being written have missing values compared to the file headers:

    • True: set missing values to null.

    • ”warn” (default): set missing values to null, log a warning.

    • False: raise an error.

Methods

__init__([file, headers, append, delay, ...])

Write CSV files with flexible header ordering and format options.

flush()

Write delayed rows if any are pending.

writerow(row)

Attributes

file

fp

Actual file pointer to the CSV file being written (available only after entering the context manager).

headers

name

Name of the CSV file (<temp> for a temporary file, <{io name}> for an IO object).

no_headers

path

Path to the CSV file (if writing to a temporary file, this is available only after entering the context manager.

rowcount

flush()

Write delayed rows if any are pending.

This method is called automatically: 1. As soon as headers are set, 2. When exiting the context manager.

If headers were not already set (case 2.), they are determined from the delayed rows and written.

property fp

Actual file pointer to the CSV file being written (available only after entering the context manager).

property name: str

Name of the CSV file (<temp> for a temporary file, <{io name}> for an IO object).

property path: Path

Path to the CSV file (if writing to a temporary file, this is available only after entering the context manager. Not available for IO objects).