最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python字节单位如何转换 Python字节单位转换代码实例
时间:2019-12-05 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python字节单位转换代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
具体代码如下:
from enum import Enum class Values(): values={'B':1} @staticmethod def getValues(): if len(Values.values)<=1: kbunits=['KB','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB'] kibunits=['KiBi','MiB','GiB','TiB','PiB','EiB','ZiB','YiB','BiB','NiB','DiB'] for index,unit in enumerate(kibunits): Values.values[unit]=1<<(index+1)*10 for index,unit in enumerate(kbunits): Values.values[unit]=10**((index+1)*3) return Values.values @staticmethod def get(key): return Values.getValues().get(key) class Units(Enum): def __new__(cls,name): obj = object.__new__(cls) print (name) obj._value_=Values.get(name) return obj B=('B') KB=('KB') KiB=('KiBi') MB=('MB') MiB=('MiB') GB=('GB') GiB=('GiB') TB=('TB') TiB=('TiB') PB=('PB') PiB=('PiB') EB=('EB') EiB=('EiB') ZB=('ZB') ZiB=('ZiB') YB=('YB') YiB=('YiB') BB=('BB') BiB=('BiB') NB=('NB') NiB=('NiB') DB=('DB') DiB=('DiB') class ByteUnitConversionUtil(): __defaultformat="%.5f" @staticmethod def convert(value,unit=Units.B,format=__defaultformat): if(unit==Units.B): return str(value).split(".",2)[0]+unit.name else: return (format % (value/unit.value))+unit.name if __name__=="__main__": print(ByteUnitConversionUtil.convert(12313213453,Units.KiB,"%.2f"))
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27