Python - csv.DictWriter()或writerow()舍入我的浮动? [英] Python - csv.DictWriter() or writerow() rounding my floats?
问题描述
我是Python的新手,所以如果这个问题很容易回答,如果我的Python语言有点离题,请原谅我。
我有一个字典我想写一个csv文件,但字典中的浮动在我写它们到文件四舍五入。
舍入发生在哪里,如何防止?
提前感谢!
ps我按照这里的DictWriter示例: http://www.doughellmann.com/PyMOTW/csv/我在Mac上运行Python 2.6.1(10.6 - Snow Leopard)。
#my import statement
import sys
import csv
是我的字典(d)包含:
>> d = runtime .__ dict__
>>>> d
{'time_final':1323494016.8556759,
'time_init':1323493818.0042379,
'time_lapsed':198.85143804550171}
这些值确实是浮点数:
>> type(runtime.time_init)
< type'float'>
然后我设置我的writer并写头和值:
f = open(log_filename,'w')
fieldnames =('time_init','time_final','time_lapsed')
myWriter = csv.DictWriter(f,fieldnames = fieldnames)
header = 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 source ,这似乎是硬连线的行为。一个解决方法是在csv获取它之前将所有的float值转换到它们的repr。使用类似: d = dict((k,repr(v))for k,v in d.items())
。
下面是一个实例:
import sys,csv
d = {'time_final':1323494016.8556759,
'time_init':1323493818.0042379,
'time_lapsed':198.85143804550171
}
d = dict((k,repr对于k,v在d.items())
fieldnames =('time_init','time_final','time_lapsed')
myWriter = csv.DictWriter(sys.stdout,fieldnames =字段名)
header = 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 isinstance(v,float)
注意,I '只是固定这个问题为Py2.7.3,所以它不应该是一个问题在未来。请参见 http://hg.python.org/cpython/rev/bf7329190ca6
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) <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
这篇关于Python - csv.DictWriter()或writerow()舍入我的浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!