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 startedHas 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.
- Define a function named
parse_log_linethat takesline. Inside, assign the result ofline.split(": ", 1)totimestampandmessage. Then assign the result oftimestamp.split(" ")todateandtime. Returndate,time, andmessage.strip(). - Open
log.txtin read mode. Write aforloop overfile. Inside, callparse_log_line(line)and assign the result todate,time, andmessage. Callprint(f"{date} {time} -> {message}").
# log.txt contains: # 2024-01-15 10:30: Server started # 2024-01-15 10:31: Connected 5 clients # 2024-01-15 10:32: Request received # Step 1: Define parse_log_line(line) — split on ": " then split timestamp on " " # Step 2: Open log.txt, loop over lines, parse each line, and print
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding