Processing Structured Text Files
A line read from a text file usually contains an invisible newline character at its end.
A Line Has More Than You See
A line read from a text file usually contains an invisible newline character at its end. That character is part of the string even though it is not visible on the screen. If you pass the line directly to print(), the existing newline and print()'s automatic newline work together, creating an empty line between displayed lines.
What do you think happens?
Suppose line contains the text From: sender followed by a newline character. What happens when print(line) runs?
Reveal answer
The text is followed by an extra blank line.
The newline already stored in the file line moves to the next line, and print() adds another newline after printing the string.
Prefix Checks with startswith()
The startswith() method checks whether a string begins with a specified prefix. It returns True when the beginning matches and False otherwise. It does not search for the prefix anywhere else in the string.
- The first string begins with From:, so the check returns True.
- The second string contains similar text later, but it does not begin with From:, so the check returns False.
True
FalseThis distinction matters when a file uses prefixes to identify different kinds of lines. For example, a structured text file might use From:, Subject:, or Date: at the beginning of lines. A conditional using startswith() can select only the kind of line you need instead of matching the same text when it appears in the middle or at the end.
Removing Trailing Characters
The rstrip() method removes trailing whitespace and newline characters from a string. It operates at the end of the string, so the meaningful text at the beginning remains unchanged. It can remove multiple trailing whitespace characters, including spaces, tabs, or newlines.
- The original line ends with spaces and a newline.
rstrip()removes those trailing characters.- The text Subject: Report remains.
The Filtering Pipeline
The useful workflow combines both methods. For each line, first remove trailing whitespace with rstrip(). Then check the cleaned line with startswith(). If the Boolean result is True, display the line. If it is False, skip it. Calling rstrip() first creates a clean value for both the test and the output, even though a newline at the end would not change a prefix check at the beginning.
for line in file:
clean_line = line.rstrip()
if clean_line.startswith("From:"):
print(clean_line)- 1The for loop processes each line from the file.
- 2
rstrip()removes trailing whitespace and the newline character. - 3
startswith("From:")checks only the beginning of the cleaned line. - 4
print()displays the line only when the prefix check returns True.
From: sender@example.com
From: archive@example.comIn this generated example, lines beginning with Subject: or other text are not displayed. The selected From: lines appear with single spacing because rstrip() removed their stored newline before print() added its own.
Structured Text in Practice
Structured text files often give different meanings to lines based on their prefixes. Email files can use prefixes such as From:, Subject:, To:, and Date:. Log files can use ERROR:, WARNING:, or INFO: to mark severity levels. The same processing pattern works in both cases: clean each line, test its prefix, and display or process only the lines that match.
Selecting Warning Lines
Process lines from a log-like file and display only lines that begin with WARNING:.
- 1Clean
Call
rstrip()on the line so the trailing newline and other trailing whitespace do not remain in the displayed result. - 2Check
Call
startswith("WARNING:")on the cleaned line. The result is True only when WARNING: is at the beginning. - 3Display
Use
print()inside the conditional so only matching lines are displayed.
The workflow extracts WARNING: lines and displays them without the extra blank lines caused by printing the original file lines.
Mistakes to Avoid
Common mistake Treating startswith() as a general search method
What it looks like"Message received from: sender".startswith("From:")Why it failsThe target text is not at the beginning of the string, so
startswith()returns False.Do this insteadUse startswith() when the prefix position matters. It checks only the beginning of the string.
Common mistake Printing the original file line directly
What it looks likeprint(line)Why it failsThe file line already contains a newline, and
print()adds another newline.Do this insteadCall line.rstrip() before printing.Common mistake Using rstrip() after the line has already been printed
What it looks likeprint(line) clean_line = line.rstrip()Why it failsThe extra spacing has already occurred by the time the cleaned value is created.
Do this insteadClean the line first, then pass the cleaned value to startswith() and print().
Common mistake Checking one value but displaying a different uncleaned value
What it looks likeclean_line = line.rstrip() if clean_line.startswith("From:"): print(line)Why it failsThe condition uses the cleaned line, but the output still contains the original trailing newline.
Do this insteadPrint clean_line so the value tested and displayed has the same cleaned form.
Practice the Pattern
Write a loop that processes each line in file and displays only lines beginning with Subject:. Remove trailing whitespace before checking and printing the line.
Hints
- Create a cleaned value by calling
rstrip()on each line. - Use
startswith("Subject:")as the conditional test. - Pass the cleaned value to
print().
- Each file line is cleaned before it is tested.
- Only lines beginning with Subject: reach
print(). - The cleaned value is printed, preventing double spacing.
Key Takeaways
- 1
startswith()checks only whether a string begins with a specified prefix and returns True or False. - 2A line read from a file includes a trailing newline, while
print()adds another newline, which can create double spacing. - 3
rstrip()removes trailing whitespace and newline characters from the end of a string. - 4A reliable filtering pattern is to call
rstrip(), test the result withstartswith(), and print the cleaned line when the test is True. - 5Prefix-based filtering is useful for structured text such as email-style files and log files.
At a glance
- Use
startswith()to recognize lines by their beginning, not by text appearing anywhere. - Remove a file line's trailing newline with
rstrip()before displaying it. - Clean each line first, then check its prefix and print only matching lines.
- This pattern supports practical filtering of structured email and log text files.