Python字節(jié)單位轉(zhuǎn)換(將字節(jié)轉(zhuǎn)換為K M G T)
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編寫(xiě)的儲(chǔ)存單位轉(zhuǎn)換代碼(以字節(jié)(B)為單位)
def bytes(bytes):
if bytes < 1024: #比特
bytes = str(round(bytes, 2)) + ' B' #字節(jié)
elif bytes >= 1024 and bytes < 1024 * 1024:
bytes = str(round(bytes / 1024, 2)) + ' KB' #千字節(jié)
elif bytes >= 1024 * 1024 and bytes < 1024 * 1024 * 1024:
bytes = str(round(bytes / 1024 / 1024, 2)) + ' MB' #兆字節(jié)
elif bytes >= 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024:
bytes = str(round(bytes / 1024 / 1024 / 1024, 2)) + ' GB' #千兆字節(jié)
elif bytes >= 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024:
bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024, 2)) + ' TB' #太字節(jié)
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' #拍字節(jié)
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' #艾字節(jié)
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))
測(cè)試:
"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
到此這篇關(guān)于Python字節(jié)單位轉(zhuǎn)換(將字節(jié)轉(zhuǎn)換為K M G T)的文章就介紹到這了,更多相關(guān)Python字節(jié)單位轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于python安裝第三方庫(kù)的問(wèn)題與解決方案
Python開(kāi)發(fā)中經(jīng)常遇到第三方庫(kù)安裝困難的問(wèn)題,速度慢可以使用國(guó)內(nèi)鏡像如清華鏡像加速,若遇到wheel錯(cuò)誤,可以手動(dòng)下載whl文件進(jìn)行安裝,對(duì)于找不到的包,可以嘗試在Python的官方包發(fā)布網(wǎng)站上進(jìn)行搜索和下載,本文提供了具體的解決方案和操作步驟2024-10-10
Django 使用easy_thumbnails壓縮上傳的圖片方法
今天小編就為大家分享一篇Django 使用easy_thumbnails壓縮上傳的圖片方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Python開(kāi)發(fā)必知必會(huì)標(biāo)識(shí)符UUID全面使用指南
在Python編程中,UUID(通用唯一標(biāo)識(shí)符)是一個(gè)非常有用的工具,用于生成唯一的標(biāo)識(shí)符,本文將深入探討Python中UUID的用法、不同版本的UUID、以及如何在實(shí)際應(yīng)用中充分利用UUID的優(yōu)勢(shì)2023-12-12

