Python字节单位转换(将字节转换为K M G T)


Posted in Python onMarch 02, 2021
def bytes_to_human(n):
  symbols = ('K','M','G','T','P','E','Z','Y')
  prefix = {}
  for i,s in enumerate(symbols):
    prefix[s] = 1 << (i + 1) * 10
  for s in reversed(symbols):
    if n >= prefix[s]:
      value = float(n) / prefix[s]
      return '%.1f%s' % (value,s)
  return '%sB' % n

python编写的储存单位转换代码(以字节(B)为单位)

def bytes(bytes):
  if bytes < 1024: #比特
    bytes = str(round(bytes, 2)) + ' B' #字节
  elif bytes >= 1024 and bytes < 1024 * 1024:
    bytes = str(round(bytes / 1024, 2)) + ' KB' #千字节
  elif bytes >= 1024 * 1024 and bytes < 1024 * 1024 * 1024:
    bytes = str(round(bytes / 1024 / 1024, 2)) + ' MB' #兆字节
  elif bytes >= 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024:
    bytes = str(round(bytes / 1024 / 1024 / 1024, 2)) + ' GB' #千兆字节
  elif bytes >= 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024:
    bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024, 2)) + ' TB' #太字节
  elif bytes >= 1024 * 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024 * 1024:
    bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024 / 1024, 2)) + ' PB' #拍字节
  elif bytes >= 1024 * 1024 * 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024:
    bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024 / 1024 /1024, 2)) + ' EB' #艾字节
  return bytes
 
if __name__ == '__main__':
  print('0:' + bytes(0))
  print('1:' + bytes(1))
  print('2:' + bytes(10))
  print('3:' + bytes(100))
  print('4:' + bytes(1000))
  print('5:' + bytes(10000))
  print('6:' + bytes(100000))
  print('7:' + bytes(1000000))
  print('8:' + bytes(10000000))
  print('9:' + bytes(100000000))
  print('10:' + bytes(1000000000))
  print('11:' + bytes(10000000000))
  print('12:' + bytes(100000000000))
  print('13:' + bytes(1000000000000))
  print('14:' + bytes(10000000000000))
  print('15:' + bytes(100000000000000))
  print('16:' + bytes(1000000000000000))
  print('17:' + bytes(10000000000000000))
  print('18:' + bytes(100000000000000000))
  print('19:' + bytes(1000000000000000000))
  print('20:' + bytes(10000000000000000000))
  print('20:' + bytes(100000000000000000000))
  print('20:' + bytes(1000000000000000000000))

测试:

"D:\Program Files\Python\Python36\python.exe" C:/Users/Jochen/PycharmProjects/mysite/bytes.py
0:0 B
1:1 B
2:10 B
3:100 B
4:1000 B
5:9.77 KB
6:97.66 KB
7:976.56 KB
8:9.54 MB
9:95.37 MB
10:953.67 MB
11:9.31 GB
12:93.13 GB
13:931.32 GB
14:9.09 TB
15:90.95 TB
16:909.49 TB
17:8.88 PB
18:88.82 PB
19:888.18 PB
20:8.67 EB
20:86.74 EB
20:867.36 EB

Process finished with exit code 0

到此这篇关于Python字节单位转换(将字节转换为K M G T)的文章就介绍到这了,更多相关Python字节单位转换内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
在 Django/Flask 开发服务器上使用 HTTPS
Jul 03 Python
python实现的文件夹清理程序分享
Nov 22 Python
纯python实现机器学习之kNN算法示例
Mar 01 Python
numpy中实现二维数组按照某列、某行排序的方法
Apr 04 Python
python 实现在Excel末尾增加新行
May 02 Python
解决使用PyCharm时无法启动控制台的问题
Jan 19 Python
numpy.where() 用法详解
May 27 Python
对python中的装包与解包实例详解
Aug 24 Python
python将print输出的信息保留到日志文件中
Sep 27 Python
Django Docker容器化部署之Django-Docker本地部署
Oct 09 Python
Python imutils 填充图片周边为黑色的实现
Jan 19 Python
解决tensorflow 释放图,删除变量问题
Jun 23 Python
Python使用cn2an实现中文数字与阿拉伯数字的相互转换
Mar 02 #Python
jupyter notebook指定启动目录的方法
Mar 02 #Python
python实现发送邮件
Mar 02 #Python
matplotlib阶梯图的实现(step())
Mar 02 #Python
Python读写Excel表格的方法
Mar 02 #Python
Python绘制K线图之可视化神器pyecharts的使用
Mar 02 #Python
python中Pexpect的工作流程实例讲解
Mar 02 #Python
You might like
如何对PHP程序中的常见漏洞进行攻击
2006/10/09 PHP
php mysql数据库操作类
2008/06/04 PHP
javascript 跳转代码集合
2009/12/03 Javascript
jquery 事件执行检测代码
2009/12/09 Javascript
javascript中字符串拼接需注意的问题
2010/07/13 Javascript
Web 前端设计模式--Dom重构 提高显示性能
2010/10/22 Javascript
a标签的href和onclick 的事件的区别介绍
2013/07/26 Javascript
js实现带按钮的上下滚动效果
2015/05/12 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
JS实现非首屏图片延迟加载的示例
2018/01/06 Javascript
Vue.use源码学习小结
2018/06/20 Javascript
bootstrap 弹出框modal添加垂直方向滚轴效果
2018/07/09 Javascript
初探Vue3.0 中的一大亮点Proxy的使用
2018/12/06 Javascript
详解关于微信setData回调函数中的坑
2019/02/18 Javascript
对layui中的onevent 和event的使用详解
2019/09/06 Javascript
vue+element导航栏高亮显示的解决方式
2019/11/12 Javascript
JQuery+drag.js上传图片并且实现图片拖曳
2020/11/18 jQuery
理解python正则表达式
2016/01/15 Python
python中学习K-Means和图片压缩
2017/11/20 Python
python实现简单淘宝秒杀功能
2018/05/03 Python
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题
2018/09/27 Python
Centos部署django服务nginx+uwsgi的方法
2019/01/02 Python
python按修改时间顺序排列文件的实例代码
2019/07/25 Python
Python实用库 PrettyTable 学习笔记
2019/08/06 Python
Python3 翻转二叉树的实现
2019/09/30 Python
Python类super()及私有属性原理解析
2020/06/15 Python
树莓派4B安装Tensorflow的方法步骤
2020/07/16 Python
Sublime Text3最新激活注册码分享适用2020最新版 亲测可用
2020/11/12 Python
.NET概念性的面试题
2012/02/29 面试题
服务中心夜班服务员岗位职责
2013/11/27 职场文书
优秀研究生主要事迹
2014/06/03 职场文书
校庆标语集锦
2014/06/25 职场文书
英语课外活动总结
2014/08/27 职场文书
你有一份《诚信考试承诺书》待领取
2019/11/13 职场文书
奥特曼十大神器:奥特手镯在榜,第一是贝利亚的神器
2022/03/18 日漫
Tomcat项目启动失败的原因和解决办法
2022/04/20 Servers