统计平均分:从文本读取成绩并计算平均分,将平均分写入文本文件保存

75 篇文章 0 订阅

从文本读取成绩并计算平均分,将平均分写入文本文件保存。


【学习的细节是欢悦的历程】


  自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
            —— 华罗庚


等风来,不如追风去……


从文本读取成绩并计算平均分
统计平均分
(将平均分写入文本文件保存)


本文质量分:

90
本文地址: https://blog.csdn.net/m0_57158496/article/details/130546907

CSDN质量分查询入口:http://www.csdn.net/qc


目 录


◆ 统计平均分


1、题目描述


  文件 score.txt 中存储了 5 名学生的 6 门课程的成绩,共5行。读取该文件,统计每个学生的平均分,并将统计结果存入文件 score_avg.txt ,如图所示。

回页目录

2、解题


  我采用函数式编程解题,先设计一个成绩读取函数,再写一个计算平均分并写入文本文件函数。


  首选用 pandas ,当然也可以直接 Python 。(我的 Python 环境升级后,一直没 pip install pandas 成功。)


下面我就唠嗑直接 python ——

2.1 读取成绩文本,返回成绩列表

  • 常规写法

def read_score(filename: str) -> list:
    ''' 从csv文本读取成绩,返回成绩列表 '''

    with open(filename) as f:
        scores = f.read().split('\n') # 读取成绩文本数据并拆分行。
        #print(scores) # 调试用语句。

    return [list(map(int, i.split(','))) for i in scores] # 返回拆分每行成绩成列表并转整。

  • 也可以写成这样:

def read_score(filename: str) -> list:
    ''' 从csv文本读取成绩,返回成绩列表 '''
    scores = open(filename).read().split('\n') # 读取成绩文本数据并拆分行。
    #print(scores) # 调试用语句。
    return [list(map(int, i.split(','))) for i in scores] # 返回拆分每行成绩成列表并转整。

  • 函数调用

if __name__ == '__main__':
    print(read_score(f"{path}score.txt"))

  • 代码运行效果截屏图片
    在这里插入图片描述

  如图所见,代码能够成功读取成绩为列表。


回页目录

2.2 计算平均分并写入文本文件

  • 函数代码

def save_avg(filename: str, scores: list) -> None:
    ''' 保存平均分文件 '''
    with open(filename, 'w') as f:
        for i in scores:
            #f.write(str(sum(i)/len(i))) # 计算并写入文本文件。
            #f.write('\n') # 写入换行符,一个学生一行。
            f.write(f"{sum(i)/len(i):.1f}\n") # 也可以用插值字符串格式一次写入。
    print('\n平均分已成功保存到文本文件。')

  • 调用函数

if __name__ == '__main__':
    save_avg(f"{path}score_avg.txt", read_score(f"{path}score.txt"))

  • 代码运行效果截屏图片
    在这里插入图片描述

  • 平均分文本

    • 默认
      在这里插入图片描述
    • 保留一位小数
      在这里插入图片描述

  插值字符串格式写法可以更方便的设置小数位数。
  前者写法,在 str 之前可以调用 round() 保留小数,round(float, 2) 保留两位小数,round(float, 1) 保留一位小数。

如:


def save_avg(filename: str, scores: list) -> None:
    ''' 保存平均分文件 '''
    with open(filename, 'w') as f:
        for i in scores:
            f.write(str(round(sum(i)/len(i), 1))) # 计算并写入文本文件。
            f.write('\n') # 写入换行符,一个学生一行。
            #f.write(f"{sum(i)/len(i):.1f}\n") # 也可以用插值字符串格式一次写入。

    print('\n平均分已成功保存到文本文件。')



回页目录

3、完整源码

(源码较长,点此跳过源码)

#!/sur/bin/nve python
# coding: utf-8


path = r'/sdcard/Documents/'


def read_score(filename: str) -> list:
    ''' 从csv文本读取成绩,返回成绩列表 '''

    with open(filename) as f:
        scores = f.read().split('\n') # 读取成绩文本数据并拆分行。
        #print(scores) # 调试用语句。

    return [list(map(int, i.split(','))) for i in scores] # 返回拆分每行成绩成列表并转整。


def read_score(filename: str) -> list:
    ''' 从csv文本读取成绩,返回成绩列表 '''
    scores = open(filename).read().split('\n') # 读取成绩文本数据并拆分行。
    print(scores) # 调试用语句。
    return [list(map(int, i.split(','))) for i in scores] # 返回拆分每行成绩成列表并转整。


def save_avg(filename: str, scores: list) -> None:
    ''' 保存平均分文件 '''
    with open(filename, 'w') as f:
        for i in scores:
            f.write(str(round(sum(i)/len(i), 1))) # 计算并写入文本文件。
            f.write('\n') # 写入换行符,一个学生一行。
            #f.write(f"{sum(i)/len(i):.1f}\n") # 也可以用插值字符串格式一次写入。

    print('\n平均分已成功保存到文本文件。')


if __name__ == '__main__':
    save_avg(f"{path}score_avg.txt", read_score(f"{path}score.txt"))


回页首

上一篇:  统计字符中字符出现的次数(输入一个字符串,输出字符及相应字符出现的次数)
下一篇: 

我的HOT博:

  本次共计收集 201 篇博文笔记信息,总阅读量 32.59w,平均阅读量 1621。已生成 21 篇阅读量不小于 3000 的博文笔记索引链接。数据采集于 2023-05-06 05:25:55 完成,用时 4 分 44.70 秒。


  1. 让QQ群昵称色变的神奇代码
    ( 53797 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122566500
    点赞:23   踩 :0  收藏:78  打赏:0  评论:16
    本篇博文笔记于 2022-01-18 19:15:08 首发,最晚于 2022-01-20 07:56:47 修改。
  2. ChatGPT国内镜像站初体验:聊天、Python代码生成等
    ( 49298 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/129035387
    点赞:123   踩 :0  收藏:786  打赏:0  评论:75
    本篇博文笔记于 2023-02-14 23:46:33 首发,最晚于 2023-03-22 00:03:44 修改。
  3. pandas 数据类型之 DataFrame
    ( 8074 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124525814
    点赞:6   踩 :0  收藏:24  打赏:0  评论:0
    本篇博文笔记于 2022-05-01 13:20:17 首发,最晚于 2022-05-08 08:46:13 修改。
  4. 罗马数字转换器|罗马数字生成器
    ( 6404 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122592047
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2022-01-19 23:26:42 首发,最晚于 2022-01-21 18:37:46 修改。
  5. Python字符串居中显示
    ( 6104 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122163023
    点赞:1   踩 :0  收藏:5  打赏:0  评论:1
    本篇博文笔记于 2021-12-26 23:35:29 发布。
  6. 斐波那契数列的递归实现和for实现
    ( 5297 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122355295
    点赞:4   踩 :0  收藏:2  打赏:0  评论:8
    本篇博文笔记于 2022-01-06 23:27:40 发布。
  7. 个人信息提取(字符串)
    ( 5138 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124244618
    点赞:1   踩 :0  收藏:8  打赏:0  评论:0
    本篇博文笔记于 2022-04-18 11:07:12 首发,最晚于 2022-04-20 13:17:54 修改。
  8. 练习:字符串统计(坑:f‘string‘报错)
    ( 4870 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121723096
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2021-12-04 22:54:29 发布。
  9. 练习:尼姆游戏(聪明版/傻瓜式•人机对战)
    ( 4594 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121645399
    点赞:14   踩 :0  收藏:42  打赏:0  评论:0
    本篇博文笔记于 2021-11-30 23:43:17 发布。
  10. 回车符、换行符和回车换行符
    ( 4357 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123109488
    点赞:0   踩 :0  收藏:2  打赏:0  评论:0
    本篇博文笔记于 2022-02-24 13:10:02 首发,最晚于 2022-02-25 20:07:40 修改。
  11. python清屏
    ( 4293 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/120762101
    点赞:0   踩 :0  收藏:5  打赏:0  评论:0
    本篇博文笔记于 2021-10-14 13:47:21 发布。
  12. 密码强度检测器
    ( 3886 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121739694
    点赞:1   踩 :0  收藏:4  打赏:0  评论:0
    本篇博文笔记于 2021-12-06 09:08:25 首发,最晚于 2022-11-27 09:39:39 修改。
  13. 罗马数字转换器(用罗马数字构造元素的值取模实现)
    ( 3811 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122608526
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2022-01-20 19:38:12 首发,最晚于 2022-01-21 18:32:02 修改。
  14. Python列表(list)反序(降序)的7种实现方式
    ( 3649 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/128271700
    点赞:4   踩 :0  收藏:12  打赏:0  评论:8
    本篇博文笔记于 2022-12-11 23:54:15 首发,最晚于 2023-03-20 18:13:55 修改。
  15. 练习:生成100个随机正整数
    ( 3590 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122558220
    点赞:1   踩 :0  收藏:3  打赏:0  评论:0
    本篇博文笔记于 2022-01-18 13:31:36 首发,最晚于 2022-01-20 07:58:12 修改。
  16. 练习:班里有人和我同生日难吗?(概率probability、蒙特卡洛随机模拟法)
    ( 3538 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124424935
    点赞:1   踩 :0  收藏:2  打赏:0  评论:0
    本篇博文笔记于 2022-04-26 12:46:25 首发,最晚于 2022-04-27 21:22:07 修改。
  17. 我的 Python.color() (Python 色彩打印控制)
    ( 3377 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123194259
    点赞:2   踩 :0  收藏:7  打赏:0  评论:0
    本篇博文笔记于 2022-02-28 22:46:21 首发,最晚于 2022-03-03 10:30:03 修改。
  18. 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
    ( 3216 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/125415626
    点赞:3   踩 :0  收藏:4  打赏:0  评论:3
    本篇博文笔记于 2022-06-22 19:54:20 首发,最晚于 2022-06-23 22:41:33 修改。
  19. 聊天消息敏感词屏蔽系统(字符串替换 str.replace(str1, *) )
    ( 3076 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124539589
    点赞:3   踩 :0  收藏:2  打赏:0  评论:3
    本篇博文笔记于 2022-05-02 13:02:39 首发,最晚于 2022-05-21 06:10:42 修改。
  20. Linux 脚本文件第一行的特殊注释符(井号和感叹号组合)的含义
    ( 3038 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123087606
    点赞:0   踩 :0  收藏:4  打赏:0  评论:3
    本篇博文笔记于 2022-02-23 13:08:07 首发,最晚于 2022-04-04 23:52:38 修改。
  21. 练习:求列表(整数列表)平衡点
    ( 3023 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121737612
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2021-12-05 23:28:10 发布。
推荐条件 阅读量突破三千
(更多热博,请点击蓝色文字跳转翻阅)

回页首


老齐漫画头像

精品文章:

来源:老齐教室


回页首

Python 入门指南【Python 3.6.3】


好文力荐:


CSDN实用技巧博文:


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻精灵_cq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值