40 — Report — writing a summary file
One JSON file holds one kind of data; a report combines several. This guide
reads a scores file and a players file, computes a short summary — average,
top scorer, players per team — and writes one report.txt with file_write
from guide 35.
Steps
Create two data files.
scores.jsonpairs each player with a score, andplayers.jsonpairs each player with a team:
run it →[ {"name": "Mina", "score": 7}, {"name": "Sana", "score": 9}, {"name": "Junho", "score": 5} ][ {"name": "Mina", "team": "Seoul"}, {"name": "Sana", "team": "Busan"}, {"name": "Junho", "team": "Seoul"} ]Each file is a list of dicts, exactly like the
questions.jsonfrom guide 38. Thenamekeys line up, which is how the two files stay about the same players.Load both files with
json_loadfrom guide 14. Two calls, two lists:
run it →use file latest scores = json_load("scores.json") players = json_load("players.json") show f"Loaded {len(scores)} scores and {len(players)} players"It prints
Loaded 3 scores and 3 players.Walk the scores with a
forloop to total them and find the top scorer. Thebest_scorecheck remembers the biggest value seen so far:
run it →total = 0 best_name = "" best_score = -1 for p in scores: total = total + p["score"] if p["score"] > best_score: best_score = p["score"] best_name = p["name"] average = total / len(scores)This is the running-total and running-max pattern from guide 30, now walking dicts instead of plain numbers.
Count players per team with a dict. A team seen for the first time starts at 1; a team seen again goes up by one:
run it →teams = {} for p in players: team = p["team"] if team in teams: teams[team] = teams[team] + 1 else: teams[team] = 1Build the report as a list of lines and join them with
"\n".join(lines).file_write("report.txt", text)saves the whole text in one call — the same helper that saved each diary note in guide 35:
run it →lines = [] lines.append("Match report") lines.append("Players: " + str(len(players))) lines.append("Average score: " + str(average)) lines.append("Top scorer: " + best_name + " with " + str(best_score)) for team in sorted(teams): lines.append(team + ": " + str(teams[team]) + " player(s)") text = "\n".join(lines) file_write("report.txt", text)sorted(teams)prints the teams in a stable order, just like the sorted numbers in guide 39.The whole program in one file. Save
report.nme:
run it →# Report: read two JSON data files and write one text summary. # Run: nme r report # The files scores.json and players.json must exist in the same folder. use file latest scores = json_load("scores.json") players = json_load("players.json") show f"Loaded {len(scores)} scores and {len(players)} players" total = 0 best_name = "" best_score = -1 for p in scores: total = total + p["score"] if p["score"] > best_score: best_score = p["score"] best_name = p["name"] average = total / len(scores) teams = {} for p in players: team = p["team"] if team in teams: teams[team] = teams[team] + 1 else: teams[team] = 1 lines = [] lines.append("Match report") lines.append("Players: " + str(len(players))) lines.append("Average score: " + str(average)) lines.append("Top scorer: " + best_name + " with " + str(best_score)) for team in sorted(teams): lines.append(team + ": " + str(teams[team]) + " player(s)") text = "\n".join(lines) file_write("report.txt", text) show "report.txt written:" show textRun it, then look at the file it wrote:
nme r report cat report.txtLoaded 3 scores and 3 players report.txt written: Match report Players: 3 Average score: 7.0 Top scorer: Sana with 9 Busan: 1 player(s) Seoul: 2 player(s)Match report Players: 3 Average score: 7.0 Top scorer: Sana with 9 Busan: 1 player(s) Seoul: 2 player(s)The console and the file show the same report: one program turned two data files into one readable summary.
Korean uses
파일 사용 최신,json읽기, and파일쓰기. The full Korean program is in the Korean guide.
Try it yourself
Add a fourth player to both JSON files and rerun report.nme; the counts and
the average update together. Then add a lowest-score line by tracking a
worst_score exactly like the best_score check.
What you learned
json_loadreads each JSON file into its own list of dicts.- A
forloop with a running total and a running max summarizes the scores. - A dict counts groups such as players per team.
file_write("report.txt", text)saves a whole multi-line report at once.- Data files stay data; the program turns them into a report.