Python字节单位转换(将字节转换为K M G T)
作者:mighty13 发布时间:2023-11-04 02:26:18
标签:Python,字节单位转换
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 EBProcess finished with exit code 0
来源:https://blog.csdn.net/mighty13/article/details/110455560
0
投稿
猜你喜欢
- 本周暂时比较清闲,可以保持每日一更的速度。国外身份证项目新增需求,检测出身份证正面的人脸。最开始考虑mobilenet-ssd,经同事提醒,
- 看看怎样抓到你:<%Dim objCMFUDim strModifiedSet objCMFU 
- 逐步指示这是提供给更喜欢阅读的人的视频文本版本。我们将创建一个类似黑客新闻 或Reddit的社交新闻网站。它将被称为“钢铁传闻”,作为一个分
- 在读取文件时将整数变量读成了字符串, 或者需要转换列属性时,通过方法astypePython中举例:dataframe.numbers=da
- <?php function getDerivativeByFormulaAndXDATA($formula, $x_data){ $
- 由于特定需求,最近实验室需要远程连接外地的sql server 2000服务器,最开始怎么连也连不上,出现了很多问题,但是在今天上午,借用实
- 常见到网上博文有错误理论,包括身边很多朋友都曾认同“可访问性(Accessibility)是为残障人士准备”的观点。其实在互联网技术领域远不
- 系列最后一篇来说说Python中的类与对象,Python这门语言是无处不对象,如果你曾浅要了解过Python,你应该听过Python是一种面
- 一、特效预览处理前处理后细节放大后二、程序原理1.将图片转为灰白图片后,将图片分成了三块,明、暗、阴影区域2.明区域使用空白进行填充3.阴影
- 一、logging模块讲解1.函数:logging.basicConfig()参数讲解:(1)level代表高于或者等于这个值时,那么我们才
- 1.前言版本:Python3.6.1 + PyQt5 + SQL Server 2012以前一直觉得,机器学习、手写体识别这种程序都是很高大
- 图像轮廓概念轮廓是一系列相连的点组成的曲线,代表了物体的基本外形。谈起轮廓不免想到边缘,它们确实很像。简单的说,轮廓是连续的,边缘并不全都连
- 一、简单介绍正则表达式是一种小型的、高度专业化的编程语言,并不是python * 有的,是许多编程语言中基础而又重要的一部分。在python中
- 解决项目pycharm能运行,在终端却无法运行的问题报 ModuleNotFoundError: No module named '
- Python是一种非常流行的脚本语言,而且功能非常强大,几乎可以做任何事情,比如爬虫、网络工具、科学计算、树莓派、Web开发、游戏等各方面都
- 过年这段时间由于线上数据库经常压力过大导致响应非常缓慢甚至死机,咬咬牙下大决心来解决效率不高的问题!首先是由于公司秉承快速开发原则,频繁上线
- 前言常见的图像任务通常需要把照片统一成相同的格式,所以此文章正是为了统一格式而生,常见的主要有cv2和PIL.Image的相关操作,照片格式
- 在SQL Server数据库中,有min server memory与max server memory两个内存选项。数据库管理员合理设置这
- 熬了半个通宵,写出了自己的Google SiteMap文件,在这里给出详细编写教程,愿对大家有所帮助。Google SiteMap的作用及协
- 1,不用第三方库# coding: utf-8import loggingBLACK, RED, GREEN, YELLOW, BLUE,