我在 python 2.6 中使用标准 json 模块 来序列化浮点列表.但是,我得到这样的结果:
I am using the standard json module in python 2.6 to serialize a list of floats. However, I'm getting results like this:
>>> import json
>>> json.dumps([23.67, 23.97, 23.87])
'[23.670000000000002, 23.969999999999999, 23.870000000000001]'
我希望浮点数的格式只有两位小数.输出应如下所示:
I want the floats to be formated with only two decimal digits. The output should look like this:
>>> json.dumps([23.67, 23.97, 23.87])
'[23.67, 23.97, 23.87]'
我尝试定义自己的 JSON 编码器类:
I have tried defining my own JSON Encoder class:
class MyEncoder(json.JSONEncoder):
def encode(self, obj):
if isinstance(obj, float):
return format(obj, '.2f')
return json.JSONEncoder.encode(self, obj)
这适用于唯一的浮动对象:
This works for a sole float object:
>>> json.dumps(23.67, cls=MyEncoder)
'23.67'
但嵌套对象失败:
>>> json.dumps([23.67, 23.97, 23.87])
'[23.670000000000002, 23.969999999999999, 23.870000000000001]'
我不想有外部依赖,所以我更喜欢坚持使用标准的 json 模块.
I don't want to have external dependencies, so I prefer to stick with the standard json module.
我怎样才能做到这一点?
How can I achieve this?
注意:这不在任何最新版本的 Python 中都有效.
Note: This does not work in any recent version of Python.
不幸的是,我认为您必须通过猴子修补来做到这一点(在我看来,这表明标准库 json
包中的设计缺陷).例如,这段代码:
Unfortunately, I believe you have to do this by monkey-patching (which, to my opinion, indicates a design defect in the standard library json
package). E.g., this code:
import json
from json import encoder
encoder.FLOAT_REPR = lambda o: format(o, '.2f')
print(json.dumps(23.67))
print(json.dumps([23.67, 23.97, 23.87]))
发射:
23.67
[23.67, 23.97, 23.87]
如你所愿.显然,应该有一种架构化的方式来覆盖 FLOAT_REPR
,这样如果你愿意,浮点的每一个表示都在你的控制之下;但不幸的是,这不是 json
包的设计方式:-(.
as you desire. Obviously, there should be an architected way to override FLOAT_REPR
so that EVERY representation of a float is under your control if you wish it to be; but unfortunately that's not how the json
package was designed:-(.
这篇关于使用标准 json 模块格式化浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!