Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:16:50+00:00 2026-05-27T13:16:50+00:00

I have a dictionary that I want to write to a csv file, but

  • 0

I have a dictionary that I want to write to a csv file, but the floats in the dictionary are rounded off when I write them to the file. I want to keep the maximum precision.

Where does the rounding occur and how can I prevent it?

What I did

I followed the DictWriter example here and I’m running Python 2.6.1 on Mac (10.6 – Snow Leopard).


# my import statements
import sys
import csv

Here is what my dictionary (d) contains:

>>> d = runtime.__dict__
>>> d
{'time_final': 1323494016.8556759,
'time_init': 1323493818.0042379,
'time_lapsed': 198.85143804550171}

The values are indeed floats:

>>> type(runtime.time_init)
<type 'float'>

Then I setup my writer and write the header and values:

f = open(log_filename,'w')
fieldnames = ('time_init', 'time_final', 'time_lapsed')
myWriter = csv.DictWriter(f, fieldnames=fieldnames)
headers = dict( (n,n) for n in fieldnames )
myWriter.writerow(headers)
myWriter.writerow(d)
f.close()

But when I look in the output file, I get rounded numbers (i.e., floats):

time_init,time_final,time_lapsed
1323493818.0,1323494016.86,198.851438046

< EOF >

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:16:51+00:00Added an answer on May 27, 2026 at 1:16 pm

    It looks like csv is using float.__str__ rather than float.__repr__:

    >>> print repr(1323494016.855676)
    1323494016.855676
    >>> print str(1323494016.855676)
    1323494016.86
    

    Looking at the csv source, this appears to be a hardwired behavior. A workaround is to cast all of the float values to their repr before csv gets to it. Use something like: d = dict((k, repr(v)) for k, v in d.items()).

    Here’s a worked-out example:

    import sys, csv
    
    d = {'time_final': 1323494016.8556759,
         'time_init': 1323493818.0042379,
         'time_lapsed': 198.85143804550171
    }
    
    d = dict((k, repr(v)) for k, v in d.items())
    
    fieldnames = ('time_init', 'time_final', 'time_lapsed')
    myWriter = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
    headers = dict( (n,n) for n in fieldnames )
    myWriter.writerow(headers)
    myWriter.writerow(d)
    

    This code produces the following output:

    time_init,time_final,time_lapsed
    1323493818.0042379,1323494016.8556759,198.85143804550171
    

    A more refined approach will take care to only make replacements for floats:

    d = dict((k, (repr(v) if isinstance(v, float) else str(v))) for k, v in d.items())
    

    Note, I’ve just fixed this issue for Py2.7.3, so it shouldn’t be a problem in the future. See http://hg.python.org/cpython/rev/bf7329190ca6

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary of strings that i want the user to be able
I have resource dictionary files (MenuTemplate.xaml, ButtonTemplate.xaml, etc) that I want to use in
I have a Dictionary that contains items and prices. The items are unique but
I want to write a container class that acts like a dictionary (actually derives
I want to write a function that reads a file and counts the number
I have a dictionary of form data that I want to modify using a
I want to write a Visual Studio macro that switches off optimisations for a
I want to write some sort of dictionary that would accept current time and
I have a dictionary that I normally access with a key, so I need
I have a Dictionary that when I add multiple values to it, the items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.