CSV and Text
Normalize Text Fields
Clean a list of messy strings by stripping leading/trailing whitespace and
converting to lowercase. The trace shows clean resolving to the normalised
value each iteration before being appended to cleaned.
By hand
Iterate over the raw strings. For each one, assign clean = s.strip().lower()
to make the two-step cleaning explicit in the trace, then append to cleaned.
naive.py
Replay: real traced execution (multi-file project)
raw = [' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']
cleaned = []
for s in raw:
clean = s.strip().lower()
cleaned.append(clean)
print('RESULT:', cleaned)
raw ← [' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']
1raw = [' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']2cleaned = []values this step[' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']rawcleaned ← []
1raw = [' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']2cleaned = []3for s in raw:values this step[]cleaneds ← ' Alice '
2cleaned = []3for s in raw:4 clean = s.strip().lower()values this step' Alice 'sclean ← 'alice'
3for s in raw:4 clean = s.strip().lower()5 cleaned.append(clean)values this step'alice'cleancleaned ← ['alice']
4 clean = s.strip().lower()5 cleaned.append(clean)6print('RESULT:', cleaned)values this step[] → ['alice']cleaneds ← 'BOB'
2cleaned = []3for s in raw:4 clean = s.strip().lower()values this step' Alice ' → 'BOB'sclean ← 'bob'
3for s in raw:4 clean = s.strip().lower()5 cleaned.append(clean)values this step'alice' → 'bob'cleancleaned ← ['alice', 'bob']
4 clean = s.strip().lower()5 cleaned.append(clean)6print('RESULT:', cleaned)values this step['alice'] → ['alice', 'bob']cleaneds ← ' carol '
2cleaned = []3for s in raw:4 clean = s.strip().lower()values this step'BOB' → ' carol 'sclean ← 'carol'
3for s in raw:4 clean = s.strip().lower()5 cleaned.append(clean)values this step'bob' → 'carol'cleancleaned ← ['alice', 'bob', 'carol']
4 clean = s.strip().lower()5 cleaned.append(clean)6print('RESULT:', cleaned)values this step['alice', 'bob'] → ['alice', 'bob', 'carol']cleaneds ← 'DAVE '
2cleaned = []3for s in raw:4 clean = s.strip().lower()values this step' carol ' → 'DAVE 'sclean ← 'dave'
3for s in raw:4 clean = s.strip().lower()5 cleaned.append(clean)values this step'carol' → 'dave'cleancleaned ← ['alice', 'bob', 'carol', 'dave']
4 clean = s.strip().lower()5 cleaned.append(clean)6print('RESULT:', cleaned)values this step['alice', 'bob', 'carol'] → ['alice', 'bob', 'carol', 'dave']cleaneds ← ' Eve'
2cleaned = []3for s in raw:4 clean = s.strip().lower()values this step'DAVE ' → ' Eve'sclean ← 'eve'
3for s in raw:4 clean = s.strip().lower()5 cleaned.append(clean)values this step'dave' → 'eve'cleancleaned ← ['alice', 'bob', 'carol', 'dave', 'eve']
4 clean = s.strip().lower()5 cleaned.append(clean)6print('RESULT:', cleaned)values this step['alice', 'bob', 'carol', 'dave'] → ['alice', 'bob', 'carol', 'dave', 'eve']cleaneds ← 'frank '
2cleaned = []3for s in raw:4 clean = s.strip().lower()values this step' Eve' → 'frank 'sclean ← 'frank'
3for s in raw:4 clean = s.strip().lower()5 cleaned.append(clean)values this step'eve' → 'frank'cleancleaned ← ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']
4 clean = s.strip().lower()5 cleaned.append(clean)6print('RESULT:', cleaned)values this step['alice', 'bob', 'carol', 'dave', 'eve'] → ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']cleanedfor s in raw:
2cleaned = []3for s in raw:4 clean = s.strip().lower()stdout ← RESULT: ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']
5 cleaned.append(clean)6print('RESULT:', cleaned)values this stepRESULT: ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']stdout
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).
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.