实验215分:从hdfs下载成绩表对每个同学去除最高分去除最低分求总分和平均分并把处理结果上传到hdfs数据例如:10001 22 42 60 32 7710002 35 70 65 31 90处理结果:10001 134 4510002 170 57
实验步骤:
- 从hdfs下载成绩表
hdfs dfs -get /path/to/grade.txt
- 编写Python脚本,对成绩表进行处理
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
# 定义一些常量
MAX_GRADE = 100 # 最高分
MIN_GRADE = 0 # 最低分
# 定义一个函数,计算总分和平均分
def calculate_total_and_avg(grades):
total = sum(grades)
avg = round(float(total) / len(grades), 2)
return total, avg
# 读入成绩表
with open('grade.txt', 'r') as f:
lines = f.readlines()
# 处理每个同学的成绩
results = []
for line in lines:
items = line.strip().split()
student_id = items[0]
grades = [int(x) for x in items[1:]]
grades.remove(max(grades)) # 去掉最高分
grades.remove(min(grades)) # 去掉最低分
total, avg = calculate_total_and_avg(grades)
results.append((student_id, total, avg))
# 输出处理结果
with open('result.txt', 'w') as f:
for res in results:
f.write('{} {} {}\n'.format(res[0], res[1], res[2]))
- 执行Python脚本
python process_grade.py
- 将处理结果上传到hdfs
hdfs dfs -put result.txt /path/to/result.txt
``
原文地址: https://www.cveoy.top/t/topic/hbSB 著作权归作者所有。请勿转载和采集!