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科学计算环境推荐——Anaconda
Jun 30 Python
Python中使用Inotify监控文件实例
Feb 14 Python
在Python的一段程序中如何使用多次事件循环详解
Sep 07 Python
python 3.6 tkinter+urllib+json实现火车车次信息查询功能
Dec 20 Python
python实现图书管理系统
Mar 12 Python
用python写扫雷游戏实例代码分享
May 27 Python
python3实现windows下同名进程监控
Jun 21 Python
Python使用win32com模块实现数据库表结构自动生成word表格的方法
Jul 17 Python
django DRF图片路径问题的解决方法
Sep 10 Python
详解Python计算机视觉 图像扭曲(仿射扭曲)
Mar 27 Python
解决hive中导入text文件遇到的坑
Apr 07 Python
pycharm 如何查看某一函数源码的快捷键
May 12 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
BBS(php &amp; mysql)完整版(四)
2006/10/09 PHP
thinkphp 一个页面使用2次分页的实现方法
2013/07/15 PHP
PHP函数microtime()用法与说明
2013/12/04 PHP
php生成短网址示例
2014/05/05 PHP
ecshop实现smtp发送邮件
2015/02/03 PHP
PHP实现将MySQL重复ID二维数组重组为三维数组的方法
2016/08/01 PHP
PHP中时间加减函数strtotime用法分析
2017/04/26 PHP
php 生成加密公钥加密私钥实例详解
2017/06/16 PHP
获取客户端电脑日期时间js代码(jquery)
2012/09/12 Javascript
jquery的map与get方法详解
2013/11/04 Javascript
jQuery+ajax实现鼠标单击修改内容的方法
2014/06/27 Javascript
js中将String转换为number以便比较
2014/07/08 Javascript
javascript实现uploadify上传格式以及个数限制
2015/11/23 Javascript
angular ng-repeat数组中的数组实例
2017/02/18 Javascript
JS中cookie的使用及缺点讲解
2017/05/13 Javascript
JavaScript使用atan2来绘制箭头和曲线的实例
2017/09/14 Javascript
解决easyui日期时间框ie的兼容的问题
2018/03/01 Javascript
JS基于对象的链表实现与使用方法示例
2019/01/31 Javascript
JavaScript cookie原理及使用实例
2020/05/08 Javascript
js实现特别简单的钟表效果
2020/09/14 Javascript
[01:07:47]Secret vs Optic Supermajor 胜者组 BO3 第一场 6.4
2018/06/05 DOTA
将图片文件嵌入到wxpython代码中的实现方法
2014/08/11 Python
Python获取DLL和EXE文件版本号的方法
2015/03/10 Python
Python数据结构之双向链表的定义与使用方法示例
2018/01/16 Python
pandas 把数据写入txt文件每行固定写入一定数量的值方法
2018/12/28 Python
Python实现FTP弱口令扫描器的方法示例
2019/01/31 Python
Python学习笔记之函数的定义和作用域实例详解
2019/08/13 Python
Python如何在DataFrame增加数值
2020/02/14 Python
Python任务自动化工具tox使用教程
2020/03/17 Python
Python基于numpy模块实现回归预测
2020/05/14 Python
加拿大租车网站:Enterprise Rent-A-Car
2018/07/26 全球购物
澳大利亚宠物食品和用品商店:PETstock
2020/01/02 全球购物
商务英语专业求职信范文
2014/01/28 职场文书
超市督导岗位职责
2015/04/10 职场文书
CSS 新特性 contain控制页面的重绘与重排问题
2021/04/30 HTML / CSS
Mysql使用全文索引(FullText index)的实例代码
2022/04/03 MySQL