Parsing Structured Text
Exit

Parsing Structured Text

Extract data from log lines using split() and strip()

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

Structured text patterns

Log files, configuration exports, and data dumps often store information in a consistent text format. You can extract values from these patterns using .split().

A log line like this:

2024-01-15 10:30: Server started

Has a predictable structure: timestamp, then : , then the message. Splitting on ": " separates the two parts.

Splitting with a limit

.split(sep, maxsplit) stops after maxsplit splits. Use this when the message itself might contain the separator.

line = "2024-01-15 10:30: Connected: 5 clients"
timestamp, message = line.split(": ", 1)
# timestamp → "2024-01-15 10:30"
# message   → "Connected: 5 clients"

Iterating over file lines

When you iterate over an open file with for line in file:, each line includes a trailing \n. Call .strip() on the extracted message to remove it.

Instructions

Write a function that parses log lines and print the results from a file.

  1. Define a function named parse_log_line that takes line. Inside, assign the result of line.split(": ", 1) to timestamp and message. Then assign the result of timestamp.split(" ") to date and time. Return date, time, and message.strip().
  2. Open log.txt in read mode. Write a for loop over file. Inside, call parse_log_line(line) and assign the result to date, time, and message. Call print(f"{date} {time} -> {message}").