我怎样才能防止csv.DictWriter()或writerow()四舍五入我的浮动? [英] How can I prevent csv.DictWriter() or writerow() rounding my floats?
问题描述
我有一个字典,我想要写入一个csv文件,但是当我把它们写到文件中时,字典中的浮点数被四舍五入。我想保持最大的精度。
在哪里发生舍入,我该如何防止它?
我做了什么
我遵循 DictWriter示例这里,我正在Mac上运行Python 2.6.1(10.6 - Snow Leopard)。
$ b $ pre> #我的导入语句
导入sys
导入csv
这是我的字典(d)包含的内容:
>>> d =运行时.__ dict__
>>> d
{'time_final':1323494016.8556759,
'time_init':1323493818.0042379,
'time_lapsed':198.85143804550171}
这些值确实是浮点数:
>>>类型(runtime.time_init)
< type'float'>
然后我安装我的编写器并写下标题和值:
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()
但是当我查看输出文件时,即浮动):
time_init,time_final,time_lapsed
1323493818.0,1323494016.86,198.851438046
< EOF>
看起来像 csv 正在使用 float .__ str __ 而不是 float .__ repr __ :
>>> print repr(1323494016.855676)
1323494016.855676
>>> print str(1323494016.855676)
1323494016.86
查看 csv来源,这似乎是硬连线的行为。解决方法是在csv到达之前将所有浮点值转换为repr。使用类似于: d = dict((k,repr(v))for k,v in d.items())
。
以下是一个成功的例子:
$ p $ import sys,csv
d = {'time_final':1323494016.8556759,
'time_init':1323493818.0042379,
'time_lapsed':198.85143804550171
}
d = dict((k,repr(v))对于k,v在d.items())
fieldnames =('time_init','time_final','time_lapsed')
myWriter = csv.DictWriter(sys.stdout,fieldnames =字段名)
headers = dict((n,n)for n in fieldnames)
myWriter.writerow(headers)
myWriter.writerow(d)
此代码产生以下输出:
time_init,time_final,time_lapsed
1323493818.0042379,1323494016.8556759,198.85143804550171
如果只是替换浮动:
d = dict((k,(repr(v)if isins (v,float)else str(v)))for k,v in d.items())
注意,我刚刚解决了Py2.7.3的这个问题,所以在将来不应该是个问题。请参阅 http://hg.python.org/cpython/rev/bf7329190ca6
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 >
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
这篇关于我怎样才能防止csv.DictWriter()或writerow()四舍五入我的浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!