Skip to content

Support copy.replace() for csv dialects #155033

Description

@serhiy-storchaka

Feature or enhancement

There is no easy way to get a CSV dialect that differs from an existing one in a single formatting parameter. You either repeat all parameters in a csv.Dialect subclass, or pass the individual parameters to csv.reader()/csv.writer() at every call site.

I propose to support copy.replace() for dialects — both instances of csv.Dialect subclasses and the dialect objects returned by csv.get_dialect().

>>> dialect = copy.replace(csv.get_dialect('excel'), delimiter=';', strict=True)
>>> list(csv.reader(['a;b'], dialect))
[['a', 'b']]

Parameters that are not replaced are inherited from the original dialect, and the result is validated like a newly created dialect.

Note that some parameters depend on each other and may need to be replaced together. For example, replacing quotechar with None also requires replacing quoting with QUOTE_NONE, otherwise validation fails with "quotechar must be set if quoting enabled".

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    Status
    No status
    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions