Python 바이트 단위 변환(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에서 작성한 저장 단위 변환 코드(B)
def bytes(bytes):
if bytes < 1024: #
bytes = str(round(bytes, 2)) + ' B' #
elif bytes >= 1024 and bytes < 1024 * 1024:
bytes = str(round(bytes / 1024, 2)) + ' KB' #
elif bytes >= 1024 * 1024 and bytes < 1024 * 1024 * 1024:
bytes = str(round(bytes / 1024 / 1024, 2)) + ' MB' #
elif bytes >= 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024:
bytes = str(round(bytes / 1024 / 1024 / 1024, 2)) + ' GB' #
elif bytes >= 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024:
bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024, 2)) + ' TB' #
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' #
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' #
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
Python 바이트 단위 변환 (바이트를 K M G T로 변환)에 관한 이 글은 여기에 소개되었습니다. 더 많은 Python 바이트 단위 변환 내용은 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보십시오. 앞으로 많은 응원 부탁드립니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.