标签:
1 # coding: utf-8 2 3 def bytes2human(n): 4 """ 5 >>> bytes2human(10000) 6 9K 7 >>> bytes2human(100001221) 8 95M 9 """ 10 symbols = (‘K‘, ‘M‘, ‘G‘, ‘T‘, ‘P‘, ‘E‘, ‘Z‘, ‘Y‘) 11 prefix = {} 12 for i, s in enumerate(symbols): 13 prefix[s] = 1 << (i+1)*10 14 15 for s in reversed(symbols): 16 if n >= prefix[s]: 17 value = int(float(n)/prefix[s]) 18 return ‘%s%s‘ % (value, s) 19 return ‘%sB‘ % n
标签:
原文地址:http://www.cnblogs.com/liuq/p/4645488.html