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警察与小偷的实现之一客户端与服务端通信实例
Oct 09 Python
详细介绍Python中的偏函数
Apr 27 Python
Python时间模块datetime、time、calendar的使用方法
Jan 13 Python
浅谈python str.format与制表符\t关于中文对齐的细节问题
Jan 14 Python
python实现淘宝秒杀脚本
Jun 23 Python
Django中使用CORS实现跨域请求过程解析
Aug 05 Python
Python实现FLV视频拼接功能
Jan 21 Python
Python爬虫库BeautifulSoup的介绍与简单使用实例
Jan 25 Python
Python如何用filter函数筛选数据
Mar 05 Python
python为Django项目上的每个应用程序创建不同的自定义404页面(最佳答案)
Mar 09 Python
浅谈Python线程的同步互斥与死锁
Mar 22 Python
python time.strptime格式化实例详解
Feb 03 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之字符串变相相减的代码
2007/03/19 PHP
php常用表单验证类用法实例
2015/06/18 PHP
浅谈php的优缺点
2015/07/14 PHP
深入浅出讲解:php的socket通信原理
2016/12/03 PHP
php 数组元素快速去重
2017/05/05 PHP
Laravel框架实现的批量删除功能示例
2019/01/16 PHP
laravel 实现登陆后返回登陆前的页面方法
2019/10/03 PHP
PHP 实现链式操作
2021/03/09 PHP
jquery struts 验证唯一标识(公用方法)
2013/03/27 Javascript
JS 按钮点击触发(兼容IE、火狐)
2013/08/07 Javascript
页面按钮禁用与解除禁用的方法
2014/02/19 Javascript
JavaScript学习笔记之内置对象
2015/01/22 Javascript
浅谈JavaScript正则表达式分组匹配
2015/04/10 Javascript
JavaScript 实现的 zip 压缩和解压缩工具包Zip.js使用详解
2015/12/14 Javascript
浅谈JavaScript 浏览器对象
2016/06/03 Javascript
非常实用的js验证框架实现源码 附原理方法
2016/06/08 Javascript
基于javascript实现的快速排序
2016/12/02 Javascript
在Swiper内如何制作CSS3动画效果示例代码
2017/12/07 Javascript
webpack 样式加载的实现原理
2018/06/12 Javascript
angular 组件通信的几种实现方式
2018/07/13 Javascript
10行代码实现微信小程序滑动tab切换
2018/12/28 Javascript
简述Vue中容易被忽视的知识点
2019/12/09 Javascript
vue quill editor 使用富文本添加上传音频功能
2020/01/14 Javascript
[04:26]2014DOTA2西雅图国际邀请赛 总决赛TOPPLAY
2014/07/22 DOTA
[52:20]DOTA2-DPC中国联赛正赛 SAG vs XGBO3 第一场 3月5日
2021/03/11 DOTA
python逐行读取文件内容的三种方法
2014/01/20 Python
详解Python中的各种函数的使用
2015/05/24 Python
win7下python3.6安装配置方法图文教程
2018/07/31 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
2020/03/11 Python
python3 简单实现组合设计模式
2020/07/02 Python
python 利用openpyxl读取Excel表格中指定的行或列教程
2021/02/06 Python
幼儿教师师德演讲稿
2014/05/06 职场文书
单位法人授权委托书范本
2014/10/09 职场文书
研究生毕业论文导师评语
2014/12/31 职场文书
2016年社区党支部公开承诺书
2016/03/25 职场文书
2019年12月24日平安夜祝福语集锦
2019/12/24 职场文书