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编程中对Monkey Patch猴子补丁开发方式的运用
May 27 Python
Python实现字符串反转的常用方法分析【4种方法】
Sep 30 Python
Python算法输出1-9数组形成的结果为100的所有运算式
Nov 03 Python
Python实现基于KNN算法的笔迹识别功能详解
Jul 09 Python
python快排算法详解
Mar 04 Python
tensorflow指定GPU与动态分配GPU memory设置
Feb 03 Python
keras.utils.to_categorical和one hot格式解析
Jul 02 Python
Python 实现 T00ls 自动签到脚本代码(邮件+钉钉通知)
Jul 06 Python
Python实现定时监测网站运行状态的示例代码
Sep 30 Python
Python远程linux执行命令实现
Nov 11 Python
Python 数据可视化之Bokeh详解
Nov 02 Python
numpy array找出符合条件的数并赋值的示例代码
Jun 01 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+javascript模拟Matrix画面
2006/10/09 PHP
PHP session常见问题集锦及解决办法总结
2007/03/18 PHP
php 模拟get_headers函数的代码示例
2013/04/27 PHP
php编程每天必学之验证码
2016/03/03 PHP
Node.js:Windows7下搭建的Node.js服务(来玩玩服务器端的javascript吧,这可不是前端js插件)
2011/06/27 Javascript
jQuery中验证表单提交方式及序列化表单内容的实现
2014/01/06 Javascript
javascript字符串替换函数如何一次性全部替换掉
2015/10/30 Javascript
JavaScript模版引擎的基本实现方法浅析
2016/02/15 Javascript
JavaScript三种绑定事件方式及相互之间的区别分析
2017/01/10 Javascript
AngularJS中controller控制器继承的使用方法
2017/11/03 Javascript
浅谈React之状态(State)
2018/09/19 Javascript
使用vue重构资讯页面的实例代码解析
2019/11/26 Javascript
JavaScript中的this基本问题实例小结
2020/03/09 Javascript
初学Python实用技巧两则
2014/08/29 Python
分享python数据统计的一些小技巧
2016/07/21 Python
Python语言快速上手学习方法
2018/12/14 Python
Python和Java的语法对比分析语法简洁上python的确完美胜出
2019/05/10 Python
pycharm 中mark directory as exclude的用法详解
2020/02/14 Python
Python使用pickle进行序列化和反序列化的示例代码
2020/09/22 Python
CSS3实现跳动的动画效果
2016/09/12 HTML / CSS
加拿大女装网上购物:Reitmans
2016/10/20 全球购物
潘多拉珠宝英国官方网上商店:PANDORA英国
2018/06/12 全球购物
可打印的优惠券、杂货和优惠券代码:Coupons.com
2018/06/12 全球购物
联想法国官方网站:Lenovo法国
2018/10/18 全球购物
求职信写作要突出重点
2014/01/01 职场文书
生产班组长岗位职责
2014/01/05 职场文书
教师评优事迹材料
2014/01/10 职场文书
2014年五一劳动节社区活动总结
2014/04/14 职场文书
消防安全标语
2014/06/07 职场文书
中职三好学生事迹材料
2014/08/24 职场文书
党的群众路线教育实践活动领导班子整改方案
2014/10/25 职场文书
毕业论文答辩开场白和结束语
2015/05/27 职场文书
小英雄雨来观后感
2015/06/09 职场文书
CSS3实现的侧滑菜单
2021/04/27 HTML / CSS
golang特有程序结构入门教程
2021/06/02 Python
python 爬取天气网卫星图片
2021/06/07 Python