python csv模块dictwrite_Python - csv.DictWriter()或writerow()舍入我的浮动?

1586010002-jmsa.png

I'm new to Python so please forgive me if this question is easy to answer and if my Python lingo is a bit off.

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 these intact/as is.

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

Thanks in advance!

p.s. I followed the DictWriter example here: http://www.doughellmann.com/PyMOTW/csv/ 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)

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 >

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值