溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python字節(jié)單位轉(zhuǎn)換之將字節(jié)轉(zhuǎn)換為K M G T的示例

發(fā)布時間:2021-03-02 14:17:25 來源:億速云 閱讀:508 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)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編寫的儲存單位轉(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))

測試:

"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

關(guān)于“Python字節(jié)單位轉(zhuǎn)換之將字節(jié)轉(zhuǎn)換為K M G T的示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI