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 del()函数用法
Mar 24 Python
Python中文竖排显示的方法
Jul 28 Python
python+pyqt5编写md5生成器
Mar 18 Python
彻底理解Python中的yield关键字
Apr 01 Python
Python3.5内置模块之random模块用法实例分析
Apr 26 Python
使用Pandas对数据进行筛选和排序的实现
Jul 29 Python
Python数据可视化 pyecharts实现各种统计图表过程详解
Aug 15 Python
python进程间通信Queue工作过程详解
Nov 01 Python
python 利用turtle模块画出没有角的方格
Nov 23 Python
TensorFlow keras卷积神经网络 添加L2正则化方式
May 22 Python
QML用PathView实现轮播图
Jun 03 Python
用python绘制樱花树
Oct 09 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
全国FM电台频率大全 - 17 湖北省
2020/03/11 无线电
PHP 读取和修改大文件的某行内容的代码
2009/10/30 PHP
javascript 写类方式之五
2009/07/05 Javascript
FileUpload 控件 禁止手动输入或粘贴的实现代码
2010/04/07 Javascript
再探JavaScript作用域
2014/09/24 Javascript
jQuery知识点整理
2015/01/30 Javascript
Javascript动态创建表格及删除行列的方法
2015/05/15 Javascript
javascript中递归函数用法注意点
2015/07/30 Javascript
微信小程序 在Chrome浏览器上运行以及WebStorm的使用
2016/09/27 Javascript
详解js中call与apply关键字的作用
2016/11/21 Javascript
使用BootStrap实现悬浮窗口的效果
2016/12/13 Javascript
JS路由跳转的简单实现代码
2017/09/21 Javascript
AngularJs1.x自定义指令独立作用域的函数传入参数方法
2018/10/09 Javascript
浅谈ECMAScript 中的Array类型
2019/06/10 Javascript
微信域名检测接口调用演示步骤(含PHP、Python)
2019/12/08 Javascript
全网小程序接口请求封装实例代码
2020/11/06 Javascript
python抓取京东商城手机列表url实例代码
2013/12/18 Python
Python 序列化 pickle/cPickle模块使用介绍
2014/11/30 Python
Python数据结构之单链表详解
2017/09/12 Python
在python 中实现运行多条shell命令
2019/01/07 Python
详解pandas数据合并与重塑(pd.concat篇)
2019/07/09 Python
python 实现多维数组(array)排序
2020/02/28 Python
Python2及Python3如何实现兼容切换
2020/09/01 Python
简单介绍HTML5中的文件导入
2015/05/08 HTML / CSS
施华洛世奇天猫官方旗舰店:SWAROVSKI
2017/04/17 全球购物
会计电算化专业毕业生求职信范文
2013/12/10 职场文书
环境科学专业个人求职信
2013/12/15 职场文书
拖鞋店创业计划书
2014/01/15 职场文书
信息科学与技术专业求职信范文
2014/02/20 职场文书
团购业务员岗位职责
2014/03/15 职场文书
遵纪守法演讲稿
2014/05/23 职场文书
党的群众路线教育实践活动心得体会(医院)
2014/11/03 职场文书
部门经理助理岗位职责
2015/04/13 职场文书
公司员工违法违章行为检讨书
2019/06/24 职场文书
numpy数据类型dtype转换实现
2021/04/24 Python
python+opencv实现目标跟踪过程
2022/06/21 Python