搜索

python_03('python基础学习笔记')


发布时间: 2022-11-24 21:24:00    浏览次数:50 次
# 读取student_grade_input.txt文本文件内容
datas = read_file()
print("read_file datas:",datas)
# 对读取出来的内容可以进行排序
datas = sort_grades(datas)
print("sort_grades datas:",datas)
# #对排序整理过的文件我们进行输出到新的文本文档保存
write_file(datas)
#关于python中读取文件出现乱码,并且报错。
#在文本中,我们要保证统一的编码,比如都用utf-8 或者gb2312.
#统计学生高分,低分,平均分。
# 输入文件:
# 三列 学号 姓名 成绩
def compute_score():
scores = []
with open("./student_grade_input.txt",encoding="utf-8") as fin:
for line in fin:
line = line[:-1]
fields = line.split(",")
scores.append(int(fields[-1]))
max_score = max(scores)
min_score = min(scores)
avg_score = round(sum(scores)/len(scores),2)
return max_score,min_score,avg_score

max_score, min_score, avg_score = compute_score()
print(f"max_score = {max_score}, min_score = {min_score}, avg_score = {avg_score}")
# (实用技能) 统计英语文章每个单词出现的次数


word_count = {}


with open("./student_grade_output.txt") as fin:
for line in fin:
line = line[:-1]
words = line.split()
for word in words:
if word not in word_count:
word_count[word] = 0
word_count[word] += 1

print(
sorted(
word_count.items(),
key=lambda x: x[1],
reverse=True
)[:10]
)
# (实用技能)统计目录下所有文件的大小:

import os
print(os.path.getsize("student_grade_output.txt"))
sum_size = 0
for file in os.listdir("."):
if os.path.isfile(file):
sum_size += os.path.getsize(file)
print("all size of dir: ",sum_size/1000)
免责声明 python_03('python基础学习笔记'),资源类别:文本, 浏览次数:50 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 09:24:00。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/baibiaotong/p/16923440.html