這篇文章將為大家詳細(xì)講解有關(guān)Python字節(jié)單位轉(zhuǎn)換之將字節(jié)轉(zhuǎn)換為K M G T的示例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),云龍企業(yè)網(wǎng)站建設(shè),云龍品牌網(wǎng)站建設(shè),網(wǎng)站定制,云龍網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,云龍網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。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
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的示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
當(dāng)前文章:Python字節(jié)單位轉(zhuǎn)換之將字節(jié)轉(zhuǎn)換為KMGT的示例-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://jinyejixie.com/article22/dcgjcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、電子商務(wù)、小程序開發(fā)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容