🐸

The Log Fragments

The Data Tinkererpython-data-tinkerer-30-the-log-fragments
Reward: 155 XP
|

The Log Fragments

Hoppy picks up a crumpled watch log at the foot of a lookout tower. It is not a neat table and not a polished JSON record. It is just a few short lines left behind as fragments.

That is closer to what real text work feels like: imperfect, but not structureless. If you first split the text into lines, then split each line into smaller pieces, you can already pull out useful information.

Log-style text is often “short lines + repeated fields”

A light log text does not ask you to understand everything at once. It first asks you to notice the repeated shape inside each line. A line may keep repeating time, person, and action fields, only written as short fragments.

sample_line = "step=bell | keeper=Pip | room=moss-shelf"
parts = sample_line.split(" | ")
step_name = parts[0].split("=")[1]
keeper_name = parts[1].split("=")[1]
print(step_name)
print(keeper_name)

This example only shows the move of “split the line into fragments, then split on the equals sign.” It is not the exact answer to today’s starter. In the real task, you will pull a few key details from a small log-style text file.

Today’s task: build one small summary from the log fragments

The starter already reads watch_log.txt and builds log_lines. Your job is to pull out log_count, the first record’s time, the first record’s keeper, and the last record’s action, then gather them into log_summary.

1
Treat the whole log as multi-line text first

Run the starter and confirm that log_text really came from the file. Then inspect how log_lines turns the full text into separate rows.

2
Pull two key fields from the first record

The first line repeats fragments such as stamp=..., keeper=..., and action=.... Split that line with " | ", then pull first_stamp and first_keeper from the first two fragments.

3
Take one action from the last record too

Reuse the same method on the last line to get last_action. This keeps training the idea that one repeated text shape can be opened with the same repeated move.

4
Gather the result into log_summary

Finally, build one dict named log_summary from log_count, first_stamp, first_keeper, and last_action.

Keep it realistic, but still light

This is only your first contact with log-style text. We are not building a real logging system, not doing full time parsing, and not handling tricky recovery cases. The point is simpler: split a more realistic text into fragments, then pull out a few useful details.

Suggested Solution
Expand
Solution:
with open("watch_log.txt", "r", encoding="utf-8") as file:
  log_text = file.read().strip()

print("Log text:")
print(log_text)

log_lines = log_text.splitlines()
print("Log lines:", log_lines)

log_count = len(log_lines)
first_stamp = log_lines[0].split(" | ")[0].split("=")[1]
first_keeper = log_lines[0].split(" | ")[1].split("=")[1]
last_action = log_lines[2].split(" | ")[2].split("=")[1]

log_summary = {
  "log_count": log_count,
  "first_stamp": first_stamp,
  "first_keeper": first_keeper,
  "last_action": last_action,
}

print("Log count:", log_count)
print("First stamp:", first_stamp)
print("First keeper:", first_keeper)
print("Last action:", last_action)
print("Log summary:", log_summary)
Advanced Tips
Want more? Click to expand

You have now moved one step beyond “spot the pattern.” This time, you used the same habit inside a more realistic multi-line text, splitting by line and then by fragment to collect key details.

The next lesson will keep following that trail and help you pull clearer tags and signals out of mixed text.

Loading...
Terminal
Terminal
Ready to run...