zut.csv.CsvReader

class zut.csv.CsvReader(file: str | os.PathLike | IO[str], headers: Sequence[str] | HeaderParsers | Literal[False] | 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 | None = None, nullval: str | None = None, newline: str | None = None, tz: tzinfo | str | Literal['local', 'utc', ''] | None = None)

Bases: object

Read CSV files with flexible header ordering and format options.

__init__(file: str | os.PathLike | IO[str], headers: Sequence[str] | HeaderParsers | Literal[False] | 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 | None = None, nullval: str | None = None, newline: str | None = None, tz: tzinfo | str | Literal['local', 'utc', ''] | None = None)

Read CSV files with flexible header ordering and format options.

Parameters:
  • accept_additional

    Behaviour when the file being read has additional columns compared to the expected headers:

    • True: include the additional keys and values in the iterated rows.

    • ”warn” (default): discard the additional columns from the iterated rows, log a warning.

    • ”discard”: discard the additional columns from the iterated rows.

    • False: raise an error.

  • accept_missing

    Behaviour when the file being read has missing columns compared to the expected headers:

    • True: set missing values to null in the iterated rows.

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

    • False: raise an error.

Methods

__init__(file[, headers, accept_additional, ...])

Read CSV files with flexible header ordering and format options.

iter_dicts()

iter_rows()

Attributes

actual_reader

file

fp

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

headers

name

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

path

Path to the CSV file (not available for IO objects).

rowcount

Number of rows read for now.

property fp: IO[str]

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

property name: str

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

property path: Path

Path to the CSV file (not available for IO objects).

property rowcount: int

Number of rows read for now.