CSV and Text
Normalize Text Fields
Clean a list of messy strings by stripping leading/trailing whitespace and
converting to lowercase. The replay shows clean resolving to the normalised
value each iteration before being appended to cleaned.
By hand
The Pythonic way
A list comprehension chains .strip().lower() directly on each element.
Methods can be chained in any order; stripping before lowering is idiomatic
(whitespace is case-insensitive so order does not matter here).
naive.py
raw = [' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']
cleaned = []
for s in raw:
clean = s.strip().lower()
cleaned.append(clean)
print('RESULT:', cleaned)
library.py
raw = [' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']
cleaned = [s.strip().lower() for s in raw]
print('RESULT:', cleaned)
RESULT: ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']
Implementation notes
.strip()removes both leading and trailing whitespace (spaces, tabs, newlines). Use.lstrip()or.rstrip()to remove from one side only.- This pattern is typically applied after parsing a CSV column: read raw
strings with
csv.readerorcsv.DictReader, then normalise with a comprehension before further processing. - For case-insensitive comparison without permanent lowercasing, use
s.casefold()instead of.lower()— it handles Unicode edge cases (e.g. German ß) more correctly.