classification
Title: csv.writer uses str() for floats instead of repr()
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: berker.peksag, mark.dickinson, python-dev, rhettinger, terry.reedy
Priority: normal Keywords: easy

Created on 2011-12-10 18:49 by rhettinger, last changed 2016-02-28 08:54 by berker.peksag. This issue is now closed.

Messages (7)
msg149179 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-12-10 18:49
The csv.writer needs a special case for floats to print them to full precision.  See http://stackoverflow.com/a/8455313/1001643

In Py2.7, the csv.writer converts floats to strings using str().  This will store 1323494016.8556759 as '1323494016.86' and unnecessarily throw away precision.

In Py3.2, this isn't a problem because float.__str__ now returns full precision, the same a float.__repr__.
msg149221 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-12-11 10:24
+1 for fixing this.
msg149277 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-12-12 06:31
New changeset bf7329190ca6 by Raymond Hettinger in branch '2.7':
Issue #13573: The csv.writer now uses the repr() for floats rather than str().
http://hg.python.org/cpython/rev/bf7329190ca6
msg149643 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-12-16 23:31
Is this complete and ready to close, or does the doc need a note?
msg149646 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-12-17 00:01
The code is fixed.  Am leaving this open until I have a chance to see if anything in the docs need to be updated.
msg260965 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-02-28 05:00
csv.writer() documentation says:

    "All other non-string data are stringified with str() before being written."

https://docs.python.org/2.7/library/csv.html#csv.writer

I guess adding a sentence to document the special case for floats wouldn't hurt.
msg260968 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-02-28 07:35
New changeset d3ac0214b7b8 by Raymond Hettinger in branch '2.7':
Issue 13573: Document that csv.writer uses str() for floats instead of repr().
https://hg.python.org/cpython/rev/d3ac0214b7b8
History
Date User Action Args
2016-02-28 08:54:33berker.peksagsetstage: needs patch -> resolved
2016-02-28 07:35:20rhettingersetstatus: open -> closed
resolution: fixed
2016-02-28 07:35:04python-devsetmessages: + msg260968
2016-02-28 05:00:24berker.peksagsetnosy: + berker.peksag
messages: + msg260965

keywords: + easy
stage: needs patch
2011-12-17 00:01:16rhettingersetmessages: + msg149646
2011-12-16 23:31:20terry.reedysetnosy: + terry.reedy
messages: + msg149643
2011-12-12 06:31:22python-devsetnosy: + python-dev
messages: + msg149277
2011-12-11 10:24:34mark.dickinsonsetnosy: + mark.dickinson
messages: + msg149221
2011-12-10 18:49:54rhettingercreate