""" p011: Ranking of students' grades """ if __name__ == '__main__': students = [ {"sno": 101,
"sname": " Xiao Zhang ", "sgrade": 88}, {"sno": 102, "sname": " Xiao Wang ", "sgrade": 99},
{"sno": 103, "sname": " petty thief ", "sgrade": 77}, {"sno": 104, "sname": " Xiao Zhao ",
"sgrade": 66}, ] students_sort = sorted(students, key=lambda x: x["sgrade"],
reverse=True) print(f"source {students} , sort result:{students_sort}")
""" p012: Read files to sort """ def read_file(): reulst = [] with
open("./p012_student_grade.txt", encoding="utf-8") as fp: for line in fp: line
= line[:-1] line = line.split(",") line[2] = int(line[2]) reulst.append(line)
return reulst def sort_grades(datas): result = sorted(datas, key=lambda x:
x[2]) return result def wirte_file(datas): with
open("./p012_student_grade2.txt", mode="w", encoding="utf-8") as fp: for item
in datas: item[2] = str(item[2]) line = ','.join(item) + "\n" fp.write(line) if
__name__ == '__main__': # read file datas = read_file() print(datas) # Sort data datas =
sort_grades(datas) print(datas) # write file wirte_file(datas) 101, Xiao Zhang ,66 102, petty thief ,44
103, Xiao Zhou ,77 104, Xiao Zhao ,88 105, Xiao Wang ,99 106, Xiao Yu ,55 107, cockroach ,22 """ p013: Calculate the student's highest score , Lowest score , average """
def compute_score(): scores = [] with open("./p013_students_grade_file.txt",
encoding="utf-8") as fp: for line in fp: line = line[:-1] field =
line.split(',') scores.append(int(field[-1])) max_score = max(scores) min_score
= min(scores) avg_score = round(sum(scores) / len(scores), 2) return max_score,
min_score, avg_score if __name__ == '__main__': max_score, min_score, avg_score
= compute_score() print(f"mx_score = {max_score}, min_score = {min_score},
avg_score = {avg_score}") """ p014: Count the number of words in English articles """ if __name__ == '__main__':
word_count = {} with open('./p014_article.txt', encoding="utf-8") as fp: for
line in fp: 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] ) """
p015: Count the size of all files in the directory """ import os if __name__ == '__main__': sum_size = 0 for
file in os.listdir("."): if os.path.isfile(file): sum_size +=
os.path.getsize(file) print(f"all size of dir:", sum_size/1024) """
p016: Organize folders using file suffixes """ import os import shutil if __name__ == '__main__': dir =
"./p016_arrange_dir" for file in os.listdir(dir): ext =
os.path.splitext(file)[1] ext = ext[1:] if not os.path.isdir(f"{dir}/{ext}"):
os.mkdir(f"{dir}/{ext}") # print(file, ext) source_path = f"{dir}/{file}"
target_path = f"{dir}/{ext}/{file}" shutil.move(source_path, target_path)
""" p017: Recursively searches for the largest file in the directory """ import os if __name__ == '__main__': search_dir =
"E:\ study " result_files = [] for root, dirs, files in os.walk(search_dir): for
file in files: file_path = os.path.join(root, file)
result_files.append((file_path, os.path.getsize(file_path)/1024))
print(sorted(result_files, key=lambda x: x[1], reverse=True)[:10]) """
p018: Calculate the highest scores for different courses , Lowest score , average """ if __name__ == '__main__': course_grade = {} with
open("./p018_course_students_grade.txt", encoding="utf-8") as fp: for line in
fp: line = line[:-1] course, sno, sname, grade = line.split(",") #
print(course, sno, sname, grade) if course not in course_grade:
course_grade[course] = [] course_grade[course].append(int(grade)) #
print(course_grade) for course, grades in course_grade.items(): print(course,
max(grades), min(grades), round(sum(grades)/len(grades), 2)) language ,101, Xiao Zhang ,66
language ,102, petty thief ,88 language ,103, Xiao Zhou ,67 language ,104, Xiao Zhao ,89 language ,105, Xiao Wang ,75 mathematics ,101, Xiao Zhang ,87 mathematics ,102, petty thief ,55
mathematics ,103, Xiao Zhou ,99 mathematics ,104, Xiao Zhao ,48 mathematics ,105, Xiao Wang ,58 English ,101, Xiao Zhang ,64 English ,102, petty thief ,95 English ,103, Xiao Zhou ,66
English ,104, Xiao Zhao ,57 English ,105, Xiao Wang ,68 """ p019: Realize the association of different files """ if __name__ == '__main__':
course_teacher_map ={} with open("./p019_teacher_name.txt", encoding="utf-8")
as fp: for line in fp: line = line[:-1] course, teacher = line.split(",")
course_teacher_map[course] = teacher print(course_teacher_map) with
open("./p019_course_students_grade.txt", encoding="utf-8") as fp: for line in
fp: line = line[:-1] course, sno, sname, sgrade = line.split(",") teacher =
course_teacher_map[course] print(course, teacher, sno, sname, sgrade)
language ,101, Xiao Zhang ,66 language ,102, petty thief ,88 language ,103, Xiao Zhou ,67 language ,104, Xiao Zhao ,89 language ,105, Xiao Wang ,75 mathematics ,101, Xiao Zhang ,87
mathematics ,102, petty thief ,55 mathematics ,103, Xiao Zhou ,99 mathematics ,104, Xiao Zhao ,48 mathematics ,105, Xiao Wang ,58 English ,101, Xiao Zhang ,64 English ,102, petty thief ,95
English ,103, Xiao Zhou ,66 English ,104, Xiao Zhao ,57 English ,105, Xiao Wang ,68 language , Teacher Zhou mathematics , Miss Zhao English , Miss Li """
p020: Batch merge multiple text file """ import os if __name__ == '__main__': dir =
"./p020_many_texts" contents = [] for file in os.listdir(dir): file_path =
os.path.join(dir, file) if os.path.isfile(file_path) and file.endswith(".txt"):
with open(file_path, encoding="utf-8") as fp: contents.append(fp.read()) #
print(contents) final_content = "\n".join(contents) with
open(dir+"/many_texts.txt", mode="w", encoding="utf-8") as fp:
fp.write(final_content)
Technology
Daily Recommendation