Python字节单位转换(将字节转换为K M G T)
作者:mighty13 时间:2023-11-04 02:26:18
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
标签:Python,字节单位转换
0
投稿
猜你喜欢
Vue中导入excel文件的两种方式及使用步骤
2024-05-21 10:16:20
sqlserver 动态创建临时表的语句分享
2024-01-17 23:57:45
Python中常用的字典键和值排的方法
2022-12-22 15:51:05
基于webpack.config.js 参数详解
2024-05-02 16:28:30
python爬虫-模拟微博登录功能
2021-12-13 21:54:08
ASP设计常见问题及解答精要
2009-04-21 11:16:00
使用FCKeditor添加文章时,在文章最后多了逗号
2007-10-11 13:38:00
Frontpage轻松下载网页或站点
2007-10-22 13:14:00
IA学习笔记02:组织体系
2009-06-12 12:22:00
python计算牛顿迭代多项式实例分析
2022-10-27 05:32:37
Nodejs封装类似express框架的路由实例详解
2024-05-11 10:17:43
MySQL 数据库 source 命令详解及实例
2024-01-15 10:50:58
numpy np.newaxis 的实用分享
2023-01-21 06:06:25
Python logging模块handlers用法详解
2022-08-24 18:08:08
ElementUI的this.$notify.close()调用不起作用的解决
2024-05-09 09:53:16
Python基于xlrd模块处理合并单元格
2023-08-28 21:10:49
python类别数据数字化LabelEncoder VS OneHotEncoder区别
2023-10-12 07:46:46
javascript 通用滑动门tab类
2023-08-05 09:42:25
基于pip install django失败时的解决方法
2021-06-19 06:01:27
Python输入整数进行排序方式
2023-05-14 11:03:15