You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
917 B
28 lines
917 B
from collections import defaultdict
|
|
import statistics
|
|
|
|
# 创建一个字典来存储相同第四列的数据
|
|
grouped_data = defaultdict(list)
|
|
|
|
# 从 '1.log' 文件中读取数据
|
|
input_file_path = '1.log'
|
|
output_file_path = '2.log'
|
|
|
|
with open(input_file_path, 'r') as file:
|
|
for line in file:
|
|
parts = line.strip().split(',')
|
|
if len(parts) < 5: # 确保每一行至少有5个部分
|
|
continue
|
|
key = parts[4] # 第四列为键
|
|
value = int(parts[1]) # 第二列作为数值
|
|
grouped_data[key].append(value)
|
|
|
|
# 计算每个组的中位数
|
|
median_values = {key: statistics.median(values) for key, values in grouped_data.items()}
|
|
|
|
# 将结果写入到 '2.log' 文件
|
|
with open(output_file_path, 'w') as output_file:
|
|
for key, median in median_values.items():
|
|
output_file.write(f"{key}, {median}\n")
|
|
|
|
print(f"Results have been written to {output_file_path}") |