欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年03月02日 10:24:20   作者:mighty13  
這篇文章主要介紹了Python字節(jié)單位轉(zhuǎn)換(將字節(jié)轉(zhuǎn)換為K M G T),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
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)的文章就介紹到這了,更多相關(guān)Python字節(jié)單位轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 將pandas.dataframe的數(shù)據(jù)寫入到文件中的方法

    將pandas.dataframe的數(shù)據(jù)寫入到文件中的方法

    今天小編就為大家分享一篇將pandas.dataframe的數(shù)據(jù)寫入到文件中的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python中列表和字符串常用的數(shù)據(jù)去重方法總結(jié)

    Python中列表和字符串常用的數(shù)據(jù)去重方法總結(jié)

    關(guān)于數(shù)據(jù)去重,咱們這里簡單理解下,就是刪除掉重復(fù)的數(shù)據(jù),應(yīng)用的場景比如某些產(chǎn)品產(chǎn)生的大數(shù)據(jù),有很多重復(fù)的數(shù)據(jù),為了不影響分析結(jié)果,我們可能需要對這些數(shù)據(jù)進(jìn)行去重,所以本文給大家總結(jié)了Python中列表和字符串常用的數(shù)據(jù)去重方法,需要的朋友可以參考下
    2023-11-11
  • python+ffmpeg視頻并發(fā)直播壓力測試

    python+ffmpeg視頻并發(fā)直播壓力測試

    這篇文章主要為大家詳細(xì)介紹了python+ffmpeg視頻并發(fā)直播壓力測試,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Pygame實(shí)現(xiàn)小球躲避實(shí)例代碼

    Pygame實(shí)現(xiàn)小球躲避實(shí)例代碼

    大家好,本篇文章主要講的是Pygame實(shí)現(xiàn)小球躲避實(shí)例代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • pytho傳參kwargs及用法詳解

    pytho傳參kwargs及用法詳解

    這篇文章主要介紹了pytho傳參kwargs及用法,函數(shù)傳參的時(shí)候簡化傳參形式,無需指定參數(shù)名,本文結(jié)合實(shí)例代碼講解的非常詳細(xì),需要的朋友參考下吧
    2024-05-05
  • python中__call__內(nèi)置函數(shù)用法實(shí)例

    python中__call__內(nèi)置函數(shù)用法實(shí)例

    這篇文章主要介紹了python中__call__內(nèi)置函數(shù)用法,實(shí)例分析了python中__call__內(nèi)置函數(shù)的原理與使用技巧,需要的朋友可以參考下
    2015-06-06
  • python找出一個(gè)列表中相同元素的多個(gè)索引實(shí)例

    python找出一個(gè)列表中相同元素的多個(gè)索引實(shí)例

    今天小編就為大家分享一篇python找出一個(gè)列表中相同元素的多個(gè)索引實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 在TensorFlow中屏蔽warning的方式

    在TensorFlow中屏蔽warning的方式

    今天小編就為大家分享一篇在TensorFlow中屏蔽warning的方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python多版本開發(fā)環(huán)境管理工具介紹

    Python多版本開發(fā)環(huán)境管理工具介紹

    這篇文章主要介紹了Python多版本開發(fā)環(huán)境管理工具介紹的相關(guān)資料,在Python開發(fā)中,有些情況下,我們可能面臨在一臺機(jī)器上同時(shí)安裝多版本Python的需求,需要的朋友可以參考下
    2019-07-07
  • python-for x in range的用法(注意要點(diǎn)、細(xì)節(jié))

    python-for x in range的用法(注意要點(diǎn)、細(xì)節(jié))

    這篇文章主要介紹了python-for x in range的用法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05

最新評論