Skip to content

gh-155097: Add converter and formatter parameters in csv.reader and csv.writer - #155099

Open
serhiy-storchaka wants to merge 4 commits into
python:mainfrom
serhiy-storchaka:csv-converter-formatter
Open

gh-155097: Add converter and formatter parameters in csv.reader and csv.writer#155099
serhiy-storchaka wants to merge 4 commits into
python:mainfrom
serhiy-storchaka:csv-converter-formatter

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

The converter parameter of csv.reader is used instead of float() for converting unquoted fields, and the formatter parameter of csv.writer is used instead of str() for converting values to strings. Both are called with the 0-based position of the field in the row as the first argument, so the conversion can depend on the column.

… and csv.writer

They are used instead of float() and str() for converting between fields and
values.  Both take the 0-based position of the field in the row as the first
argument, so the conversion can depend on the column.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Aug 2, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34517335 | 📁 Comparing c5f3db1 against main (204feba)

  🔍 Preview build  

128 files changed · + 1 added · ± 127 modified

+ Added

± Modified

@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Could you please look at this @smontanaro?

@smontanaro

Copy link
Copy Markdown
Contributor

@serhiy-storchaka It looks fine to me. I would add the little bit of motivation you mentioned above to the descriptions of the converter and formatter arguments:

Both are called with the 0-based position of the field in the row as the first argument, so the conversion can depend on the column.

And in your original issue:

The index goes first, like in enumerate(). This also makes a wrong one-argument callable fail at once: converter=int raises TypeError on the first field instead of taking the index as the base.

I personally don't see an example where this would be useful to me. Maybe an example would be useful (I guess your money() function in the original issue would work?). Still, if this is convention for other modules' reading and writing stuff, maybe everybody already sees the value.

@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Thank you. Add the bit of motivation (why index is here) and examples. Rationale for the order of arguments is for reviewers, not end users. Users will just accept it.

And yes, the wrappers around the csv module can provide more sophisticated features -- currently they just cannot, the formatter and the converter were hardcoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review extension-modules C modules in the Modules dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants