久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用標準 json 模塊格式化浮點數

Format floats with standard json module(使用標準 json 模塊格式化浮點數)
本文介紹了使用標準 json 模塊格式化浮點數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 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 模塊格式化浮點數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 九九亚洲| 香蕉久久久久久 | 97国产精品视频 | 欧美精品一区二区三区蜜桃视频 | 成人三级在线观看 | 午夜免费影视 | 亚洲影视在线 | 性欧美精品一区二区三区在线播放 | 国产精品久久久久一区二区三区 | 日本特黄a级高清免费大片 特黄色一级毛片 | 久久精品91久久久久久再现 | 色爱av| 日韩在线不卡视频 | 欧美一级片 | 欧美精品在线一区 | 日韩av一区二区在线观看 | 久久久精品一区二区三区 | 欧美5区| www.亚洲免费 | 欧美日韩在线高清 | 欧美日韩久久 | 日本福利视频 | 日韩欧美电影在线 | 97超碰人人草 | 中文字幕成人免费视频 | 理伦毛片 | 欧美精品一区二区免费 | 日本午夜精品一区二区三区 | 国外激情av | 精品九九在线 | 欧美福利影院 | 玖玖国产 | 日韩在线综合 | 亚洲欧美精品 | 国产精品欧美一区二区三区不卡 | 在线观看涩涩视频 | 国产特黄一级 | 国产精品成人国产乱一区 | av激情在线 | 天天曰夜夜操 | 韩国主播午夜大尺度福利 |