zut.csv.CsvFormat

class zut.csv.CsvFormat(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, default_formatter: Callable[[Any], Any] | None = None, default_parser: Callable[[str], Any] | None = None)

Bases: object

Format options for CSV files.

The default format is a balanced decision between compatibility with Excel and PostgreSQL bulk COPY operation: the delimiter is set to the same locale-dependent value as Excel and encoding is set to UTF-8 with BOM, so that Excel can open the file and display accents and columns automatically. All other format options are set as expected by PostgreSQL COPY operation. Thus, the written files can be directly bulk loaded into PostgreSQL by specifying the DELIMITER option of the COPY operation and by skipping the BOM characters in the loading function.

This default format may be overridden by specifing each format option individually, or using a modified set of predefined defaults with the fmt parameter (or CSV_FORMAT environment variable). Accepted values are: - pg (or postgres, postgresql): directly bulk loadable with PostgreSQL COPY operation without specifying the delimiter or skipping the BOM characters (sets encoding=utf-8 delimiter= ). - excel (or xlsx or xls): expand the quality of Excel displaying by modify CSV values themselves (sets decimal_separator=locale, lists=visual, dicts=visual, enums=name, no_microsecond=True, no_scientific_notation=True, tz=local). - visual: sets lists=visual, dicts=visual, enums=name. - json: sets lists=json, dicts=json.

__init__(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, default_formatter: Callable[[Any], Any] | None = None, default_parser: Callable[[str], Any] | None = None)

Methods

__init__([fmt, encoding, delimiter, ...])

builtin_formatter(value)

builtin_parser(value)

escape_value(value)

examine(-> ~zut.csv.ExamineResult)

format_row(row, *[, formatters])

format_value(value, *[, formatter])

parse_row(row, *[, parsers])

parse_value(value, *[, parser])

Attributes

encoding

The character encoding used for the CSV file.

delimiter

The character used to delimit columns.

decimal_separator

The character used as the decimal separator.

nullval

The character used to represent null values.

quotechar

The character used to quote values when necessary.

newline

The character used to separate lines.

lists

The format used to write lists.

dicts

The format used to write dicts.

enums

Whether to write enum values as their names.

no_microsecond

If set, omit datetime microseconds.

no_scientific_notation

If set, disable scientific notation for numbers.

tz

If set, convert aware datetimes to naive datetimes in the given timezone when writing, and back when reading.

default_parser

Default parser to use when reading values from CSV.

default_formatter

Default formatter to use when writing values to CSV.

decimal_separator: str

The character used as the decimal separator. Defaults to ..

default_formatter: Callable[[Any], Any]

Default formatter to use when writing values to CSV.

default_parser: Callable[[str], Any]

Default parser to use when reading values from CSV.

delimiter: str

The character used to delimit columns. Defaults to get_locale_csv_delimiter().

dicts: Literal['json', 'visual']

The format used to write dicts. Defaults to json.

encoding: str

The character encoding used for the CSV file. Defaults to utf-8-sig (UTF-8 with BOM).

enums: Literal['value', 'name']

Whether to write enum values as their names. Defaults to “value”.

lists: Literal['pg', 'json', 'visual']

The format used to write lists. Defaults to pg (PostgreSQL array format).

newline: str

The character used to separate lines. Defaults to n.

no_microsecond: bool

If set, omit datetime microseconds.

no_scientific_notation: bool

If set, disable scientific notation for numbers.

nullval: str

The character used to represent null values. Defaults to an empty string (distinguished from non-null values which are expressed as two quotechars).

quotechar: str

The character used to quote values when necessary. Defaults to (double quote).

tz: tzinfo | Literal['local', 'utc'] | None

If set, convert aware datetimes to naive datetimes in the given timezone when writing, and back when reading.