如何将命令输出的字节数据转换为KB、MB或GB适配单位?
Hey there! Let's tackle converting those raw byte and packet numbers into the clean, scaled units you're looking for. First, let's break down your input: it's a space-separated string starting with the header face bytes packets, followed by groups of three values: interface name, byte count, packet count.
Step 1: The Conversion Logic
We'll scale each numeric value to the smallest unit that makes sense (KB, MB, or GB) and round to two decimal places. You can choose between binary prefixes (1KB=1024B, standard for computing) or decimal prefixes (1KB=1000B, common in networking contexts)—I'll show both options.
Step 2: Python Script for Conversion
This script takes your raw input, processes each value, and outputs the formatted string you need:
Option 1: Binary Prefixes (1KB=1024B)
def convert_to_human(size): size = int(size) kb = 1024 mb = kb * 1024 gb = mb * 1024 if size >= gb: return f"{size / gb:.2f} GB" elif size >= mb: return f"{size / mb:.2f} MB" elif size >= kb: return f"{size / kb:.2f} KB" else: return f"{size} B" # Your raw input data raw_data = "face bytes packets docker0 2341 387821 lo 208740 208740 enp2s0 53877345926 60003420684" parts = raw_data.split() # Build the formatted output formatted = parts[:3] # Keep the original header for i in range(3, len(parts), 3): interface = parts[i] bytes_formatted = convert_to_human(parts[i+1]) packets_formatted = convert_to_human(parts[i+2]) formatted.extend([interface, bytes_formatted, packets_formatted]) # Print the result print(' '.join(formatted))
Output with binary prefixes:
face bytes packets docker0 2.29 KB 378.73 KB lo 203.85 KB 203.85 KB enp2s0 50.10 GB 55.88 GB
Option 2: Decimal Prefixes (1KB=1000B)
If you prefer decimal scaling (often used in network throughput metrics), just adjust the conversion thresholds:
def convert_to_human(size): size = int(size) kb = 1000 mb = kb * 1000 gb = mb * 1000 if size >= gb: return f"{size / gb:.2f} GB" elif size >= mb: return f"{size / mb:.2f} MB" elif size >= kb: return f"{size / kb:.2f} KB" else: return f"{size} B"
Output with decimal prefixes:
face bytes packets docker0 2.34 KB 387.82 KB lo 208.74 KB 208.74 KB enp2s0 53.88 GB 60.00 GB
Step 3: Use It Directly With Your Command
If you want to pipe the output of your original command straight into this conversion, use this one-liner (binary prefixes example):
your_command_here | python3 -c " def convert_to_human(size): size = int(size) kb=1024; mb=kb*1024; gb=mb*1024 if size>=gb: return f'{size/gb:.2f} GB' elif size>=mb: return f'{size/mb:.2f} MB' elif size>=kb: return f'{size/kb:.2f} KB' else: return f'{size} B' import sys parts = sys.stdin.read().strip().split() formatted = parts[:3] for i in range(3, len(parts),3): formatted.extend([parts[i], convert_to_human(parts[i+1]), convert_to_human(parts[i+2])]) print(' '.join(formatted)) "
Just replace your_command_here with the actual command that outputs your raw stats, and you'll get the formatted results instantly.
内容的提问来源于stack exchange,提问作者john deo

