最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中如何格式化大小为人类易读的效果

Python crifan 2797浏览 0评论

折腾:

【记录】写脚本去加速处理已处理的7.3T数据

期间,需要去把当前获得的大小:

177763984

格式化输出为,容易读懂的值,比如177MB之类的

python size human readable

python – Reusable library to get human readable version of file size? – Stack Overflow

<code>def sizeof_fmt(num, suffix='B'):
    for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
        if abs(num) &lt; 1024.0:
            return "%3.1f%s%s" % (num, unit, suffix)
        num /= 1024.0
    return "%.1f%s%s" % (num, 'Yi', suffix)
</code>

去改造为:

【总结】

<code>def formatSize(sizeInBytes, decimalNum=1, isUnitWithI=False, sizeUnitSeperator=""):
  """
    format size to human readable string

    example:
      3746 -&gt; 3.7KB
      87533 -&gt; 85.5KiB
      98654 -&gt; 96.3 KB
      352 -&gt; 352.0B
      76383285 -&gt; 72.84MB
      763832854988542 -&gt; 694.70TB
      763832854988542665 -&gt; 678.4199PB

    refer:
      https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
  """
  # https://en.wikipedia.org/wiki/Binary_prefix#Specific_units_of_IEC_60027-2_A.2_and_ISO.2FIEC_80000
  # K=kilo, M=mega, G=giga, T=tera, P=peta, E=exa, Z=zetta, Y=yotta
  sizeUnitList = ['','K','M','G','T','P','E','Z']
  largestUnit = 'Y'

  if isUnitWithI:
    sizeUnitListWithI = []
    for curIdx, eachUnit in enumerate(sizeUnitList):
      unitWithI = eachUnit
      if curIdx &gt;= 1:
        unitWithI += 'i'
      sizeUnitListWithI.append(unitWithI)

    # sizeUnitListWithI = ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']
    sizeUnitList = sizeUnitListWithI

    largestUnit += 'i'

  suffix = "B"
  decimalFormat = "." + str(decimalNum) + "f" # ".1f"
  finalFormat = "%" + decimalFormat + sizeUnitSeperator + "%s%s" # "%.1f%s%s"
  sizeNum = sizeInBytes
  for sizeUnit in sizeUnitList:
      if abs(sizeNum) &lt; 1024.0:
        return finalFormat % (sizeNum, sizeUnit, suffix)
      sizeNum /= 1024.0
  return finalFormat % (sizeNum, largestUnit, suffix)


def testKb():
  kbSize = 3746
  kbStr = formatSize(kbSize)
  print("%s -&gt; %s" % (kbSize, kbStr))

def testI():
  iSize = 87533
  iStr = formatSize(iSize, isUnitWithI=True)
  print("%s -&gt; %s" % (iSize, iStr))

def testSeparator():
  seperatorSize = 98654
  seperatorStr = formatSize(seperatorSize, sizeUnitSeperator=" ")
  print("%s -&gt; %s" % (seperatorSize, seperatorStr))

def testBytes():
  bytesSize = 352
  bytesStr = formatSize(bytesSize)
  print("%s -&gt; %s" % (bytesSize, bytesStr))

def testMb():
  mbSize = 76383285
  mbStr = formatSize(mbSize, decimalNum=2)
  print("%s -&gt; %s" % (mbSize, mbStr))

def testTb():
  tbSize = 763832854988542
  tbStr = formatSize(tbSize, decimalNum=2)
  print("%s -&gt; %s" % (tbSize, tbStr))

def testPb():
  pbSize = 763832854988542665
  pbStr = formatSize(pbSize, decimalNum=4)
  print("%s -&gt; %s" % (pbSize, pbStr))


def demoFormatSize():
  testKb()
  testI()
  testSeparator()
  testBytes()
  testMb()
  testTb()
  testPb()
</code>

输出如上:

<code>3746 -&gt; 3.7KB
87533 -&gt; 85.5KiB
98654 -&gt; 96.3 KB
352 -&gt; 352.0B
76383285 -&gt; 72.84MB
763832854988542 -&gt; 694.70TB
763832854988542665 -&gt; 678.4199PB
</code>

转载请注明:在路上 » 【已解决】Python中如何格式化大小为人类易读的效果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.199 seconds, using 22.11MB memory