You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A csv.writer with quoting=csv.QUOTE_NONNUMERIC does not quote boolean values, which makes a csv.reader with the same quoting behaviour fail on that value:
$ python3 csvbug.py
Traceback (most recent call last):
File "csvbug.py", line 12, in <module>for row in reader:
ValueError: could not convert string to float: 'True'
I'd consider this inconsistency a bug, but in any case something that needs documenting.
boolean is not quoted since in Python it's a subclass of int so True and False are numeric. This is also the case with numeric objects defining __int__ or __float__ but doesn't get a corresponding string representation.
Since QUOTE_NONNUMERIC will converts data to float when reading, I think we may force the converting even when writing so the inconsistency would disappear. Or document this limitation.
This issue is not easy, it needs a thoughtful design before starting coding. I agree with Xiang's analysis and proposed solutions. But if just convert numbers to float we can get an overflow for large integers or lost precision in case of Decimal. The consumer of the CSV file may be not Python and it may support larger precision than Python float.
And it is not clear what would be better solution for enums. Should they be serialized by name or by value?
Other example is Fraction. 1/2 cannot be converted to float.
We need a parameter for the reader to control how to convert non-quoted fields to numeric. Something like parse_float in json, but it will work not only with floats.
We may add also add parameters for the reader to control how to convert numerics and other types to strings.
gh-155097 (PR #155099) makes this round-trip possible without changing the default: bool is still a number for quoting purposes, so True is still written unquoted.
A formatter writes bools in the form the consumer expects, and a converter reads them back:
Activity
tlotze commentedon Apr 11, 2017
A csv.writer with quoting=csv.QUOTE_NONNUMERIC does not quote boolean values, which makes a csv.reader with the same quoting behaviour fail on that value:
-------- csv.py ----------
I'd consider this inconsistency a bug, but in any case something that needs documenting.
zhangyangyu commentedon Apr 13, 2017
boolean is not quoted since in Python it's a subclass of int so True and False are numeric. This is also the case with numeric objects defining __int__ or __float__ but doesn't get a corresponding string representation.
Since QUOTE_NONNUMERIC will converts data to float when reading, I think we may force the converting even when writing so the inconsistency would disappear. Or document this limitation.
serhiy-storchaka commentedon Apr 19, 2017
This issue is not easy, it needs a thoughtful design before starting coding. I agree with Xiang's analysis and proposed solutions. But if just convert numbers to float we can get an overflow for large integers or lost precision in case of Decimal. The consumer of the CSV file may be not Python and it may support larger precision than Python float.
And it is not clear what would be better solution for enums. Should they be serialized by name or by value?
corona10 commentedon Apr 19, 2017
I would like to solve this issue.
Is there any other way than casting specifically for the bool object?
(e.g For general ways?)
corona10 commentedon Apr 19, 2017
Oh, I read the Serhiy Storchaka 's comment just right now.
aterrel commentedon May 21, 2025
I don't think it is reasonable to change the type system and this isn't a type bug perse, more of a difference in type expectations.
I've added the note to the docs but this is my first doc fix in CPython so not sure if I did it correctly.
pythongh-74232: Add a note about roundtrip of non-float numerics in CSV
serhiy-storchaka commentedon May 31, 2025
Other example is Fraction.
1/2cannot be converted to float.We need a parameter for the reader to control how to convert non-quoted fields to numeric. Something like
parse_floatinjson, but it will work not only with floats.We may add also add parameters for the reader to control how to convert numerics and other types to strings.
gh-74232: Add a note about roundtrip of non-float numerics in CSV (GH…
pythongh-74232: Add a note about roundtrip of non-float numerics in C…
pythongh-74232: Add a note about roundtrip of non-float numerics in C…
[3.13] gh-74232: Add a note about roundtrip of non-float numerics in …
[3.14] gh-74232: Add a note about roundtrip of non-float numerics in …
pythongh-74232: Add a note about roundtrip of non-float numerics in C…
pythongh-74232: Add a note about roundtrip of non-float numerics in C…
pythongh-74232: Add a note about roundtrip of non-float numerics in C…
serhiy-storchaka commentedon Aug 3, 2026
gh-155097 (PR #155099) makes this round-trip possible without changing the default:
boolis still a number for quoting purposes, soTrueis still written unquoted.A
formatterwrites bools in the form the consumer expects, and aconverterreads them back:The same applies to
Fraction,IntEnumand the other types in the note added by GH-134963.