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 相关文章推荐
python基础教程之元组操作使用详解
Mar 25 Python
发布你的Python模块详解
Sep 15 Python
Python 2.7中文显示与处理方法
Jul 16 Python
python查找指定文件夹下所有文件并按修改时间倒序排列的方法
Oct 21 Python
使用PIL(Python-Imaging)反转图像的颜色方法
Jan 24 Python
基于sklearn实现Bagging算法(python)
Jul 11 Python
python通过TimedRotatingFileHandler按时间切割日志
Jul 17 Python
python处理excel绘制雷达图
Oct 18 Python
python json load json 数据后出现乱序的解决方案
Feb 27 Python
Python 中 sorted 如何自定义比较逻辑
Feb 02 Python
python Autopep8实现按PEP8风格自动排版Python代码
Mar 02 Python
用Python实现Newton插值法
Apr 17 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压缩文件夹最新版
2018/07/18 PHP
PHP 进程池与轮询调度算法实现多任务的示例代码
2019/11/26 PHP
关于IE、Firefox、Opera页面呈现异同 写脚本很痛苦
2009/08/28 Javascript
javascript 获取select下拉列表值的代码
2009/09/07 Javascript
深入理解JavaScript系列(1) 编写高质量JavaScript代码的基本要点
2012/01/15 Javascript
JS模板实现方法
2013/04/03 Javascript
JavaScript实现下拉菜单的显示和隐藏
2016/01/05 Javascript
用canvas 实现个图片三角化(LOW POLY)效果
2016/02/18 Javascript
JS留言功能的简单实现案例(推荐)
2016/06/23 Javascript
jQuery简单创建节点的方法
2016/09/09 Javascript
使用 vue-i18n 切换中英文效果
2018/05/23 Javascript
vue-cli3跨域配置的简单方法
2019/09/06 Javascript
Vuex中实现数据状态查询与更改
2019/11/08 Javascript
Vue+Node服务器查询Mongo数据库及页面数据传递操作实例分析
2019/12/20 Javascript
关于vue属性使用和不使用冒号的区别说明
2020/10/22 Javascript
Python实现将绝对URL替换成相对URL的方法
2015/06/28 Python
python3利用ctypes传入一个字符串类型的列表方法
2019/02/12 Python
详解Django项目中模板标签及模板的继承与引用(网站中快速布置广告)
2019/03/27 Python
Django实现基于类的分页功能
2019/10/31 Python
wxpython绘制音频效果
2019/11/18 Python
简单了解为什么python函数后有多个括号
2019/12/19 Python
移动端html5判断是否滚动到底部并且下拉加载
2019/11/19 HTML / CSS
HashMap和Hashtable的区别
2013/05/18 面试题
介绍一下write命令
2014/08/10 面试题
中专生自我鉴定书范文
2013/12/28 职场文书
计算机专业职业规划
2014/02/28 职场文书
志愿者爱心公益活动策划方案
2014/09/15 职场文书
报表员工作失误检讨书范文
2014/09/19 职场文书
一份关于丢失公司财物的检讨书
2014/09/19 职场文书
干部作风建设个人剖析材料
2014/10/11 职场文书
教师节慰问信
2015/02/15 职场文书
音乐教师求职信范文
2015/03/20 职场文书
如何书写读后感?(附范文)
2019/07/26 职场文书
《悬崖边的树》读后感2篇
2019/12/02 职场文书
python之django路由和视图案例教程
2021/07/26 Python
apache虚拟主机配置的三种方式(小结)
2022/07/23 Servers