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 16 Python
简单谈谈Python中的元祖(Tuple)和字典(Dict)
Apr 21 Python
详解Python3.6安装psutil模块和功能简介
May 30 Python
Python中pandas模块DataFrame创建方法示例
Jun 20 Python
python读取一个目录下所有txt里面的内容方法
Jun 23 Python
Python使用装饰器模拟用户登陆验证功能示例
Aug 24 Python
python获取Pandas列名的几种方法
Aug 07 Python
在Django下测试与调试REST API的方法详解
Aug 29 Python
python 正则表达式贪婪模式与非贪婪模式原理、用法实例分析
Oct 14 Python
keras中的loss、optimizer、metrics用法
Jun 15 Python
Python eval函数原理及用法解析
Nov 14 Python
20行代码教你用python给证件照换底色的方法示例
Feb 05 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
一个用于mysql的数据库抽象层函数库
2006/10/09 PHP
php微信开发之批量生成带参数的二维码
2016/06/26 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
2017/09/22 PHP
PHP开发之归档格式phar文件概念与用法详解【创建,使用,解包还原提取】
2017/11/17 PHP
记Laravel调用Gin接口调用formData上传文件的实现方法
2019/12/12 PHP
使用简洁的jQuery方法实现隔行换色功能
2014/01/02 Javascript
jquery事件的ready()方法使用详解
2015/11/11 Javascript
动态加载js、css的简单实现代码
2016/05/26 Javascript
jQuery中deferred对象使用方法详解
2016/07/14 Javascript
浅谈jQuery before和insertBefore的区别
2016/12/04 Javascript
Javascript oop设计模式 面向对象编程简单实例介绍
2016/12/13 Javascript
5种JavaScript脚本加载的方式
2017/01/16 Javascript
Angualrjs和bootstrap相结合实现数据表格table
2017/03/30 Javascript
Angular 2父子组件数据传递之@Input和@Output详解 (上)
2017/07/05 Javascript
Vue 2.5 Level E 发布了: 新功能特性一览
2017/10/24 Javascript
解决Jstree 选中父节点时被禁用的子节点也会选中的问题
2017/12/27 Javascript
Node.js笔记之process模块解读
2018/05/31 Javascript
Vue中对iframe实现keep alive无刷新的方法
2019/07/23 Javascript
序列化模块json代码实例详解
2020/03/03 Javascript
查找Vue中下标的操作(some和findindex)
2020/08/12 Javascript
vue实践---根据不同环境,自动转换请求的url地址操作
2020/09/21 Javascript
Python Tkinter简单布局实例教程
2014/09/03 Python
详解Django中的权限和组以及消息
2015/07/23 Python
详解在Python程序中解析并修改XML内容的方法
2015/11/16 Python
利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例
2017/08/08 Python
Python实现的简单计算器功能详解
2018/08/25 Python
python中sys模块是做什么用的
2020/08/16 Python
处理HTML5新标签的浏览器兼容版问题
2017/03/13 HTML / CSS
物流专业毕业生推荐信范文
2013/11/18 职场文书
高中毕业的自我鉴定
2013/12/09 职场文书
服务标语口号
2014/07/01 职场文书
解除劳动合同证明书
2014/09/26 职场文书
敬老院志愿者活动总结
2015/05/06 职场文书
中秋节感想
2015/08/10 职场文书
工作总结之小学教师体育工作范文(3篇)
2019/10/07 职场文书
pytorch 一行代码查看网络参数总量的实现
2021/05/12 Python