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在命令行下使用google翻译(带语音)
Jan 16 Python
Python入门篇之面向对象
Oct 20 Python
Python实现从订阅源下载图片的方法
Mar 11 Python
Ruby使用eventmachine为HTTP服务器添加文件下载功能
Apr 20 Python
python将ansible配置转为json格式实例代码
May 15 Python
matplotlib.pyplot画图 图片的二进制流的获取方法
May 24 Python
CentOS7下python3.7.0安装教程
Jul 30 Python
Python用Try语句捕获异常的实例方法
Jun 26 Python
Python 正则表达式 re.match/re.search/re.sub的使用解析
Jul 22 Python
基于python-pptx库中文文档及使用详解
Feb 14 Python
Django实现简单的分页功能
Feb 22 Python
Python TypeError: ‘float‘ object is not subscriptable错误解决
Dec 24 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
《破坏领主》销量已超100万 未来将继续开发新内容
2020/03/08 其他游戏
过滤掉PHP数组中的重复值的实现代码
2011/07/17 PHP
php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍
2013/06/18 PHP
php操作xml入门之xml基本介绍及xml标签元素
2015/01/23 PHP
PHP输出一个等腰三角形的方法
2015/05/12 PHP
JS查看对象功能代码
2008/04/25 Javascript
js电信网通双线自动选择技巧
2008/11/18 Javascript
基于jQuery的实现简单的分页控件
2010/10/10 Javascript
js实现遮罩层划出效果是生成div而不是显示
2014/07/29 Javascript
CKEditor无法验证的解决方案(js验证+jQuery Validate验证)
2016/05/09 Javascript
jQuery点击导航栏选中更换样式的实现代码
2017/01/23 Javascript
jQuery鼠标移动图片上实现放大效果
2017/06/25 jQuery
jQuery绑定事件方法及区别(bind,click,on,live,one)
2017/08/14 jQuery
JavaScript实现获取select下拉框中第一个值的方法
2018/02/06 Javascript
jQuery实现的简单对话框拖动功能示例
2018/06/05 jQuery
解决vue2.0 element-ui中el-upload的before-upload方法返回false时submit()不生效问题
2018/08/24 Javascript
详解JS中统计函数执行次数与执行时间
2018/09/04 Javascript
Vue实现调节窗口大小时触发事件动态调节更新组件尺寸的方法
2018/09/15 Javascript
js canvas画布实现高斯模糊效果
2018/11/27 Javascript
vue实现移动端悬浮窗效果
2018/12/01 Javascript
python如何在列表、字典中筛选数据
2018/03/19 Python
Python实现OpenCV的安装与使用示例
2018/03/30 Python
利用Python如何制作好玩的GIF动图详解
2018/07/11 Python
Python解决两个整数相除只得到整数部分的实例
2018/11/10 Python
Django文件存储 默认存储系统解析
2019/08/02 Python
python 字段拆分详解
2019/12/17 Python
python tkinter之顶层菜单、弹出菜单实例
2020/03/04 Python
Python json读写方式和字典相互转化
2020/04/18 Python
序列化Python对象的方法
2020/08/01 Python
求职自荐信格式
2013/12/04 职场文书
淘宝网店营销策划书
2014/01/11 职场文书
揭牌仪式策划方案
2014/05/28 职场文书
采购员岗位职责范本
2015/04/07 职场文书
女性健康知识讲座主持词
2015/07/04 职场文书
教师素质教育心得体会
2016/01/19 职场文书
Python借助with语句实现代码段只执行有限次
2022/03/23 Python