Python’s translate method returns a new string after transforming or removing characters according to a translation table. Use str.maketrans to create mappings, then pass the table to translate. section 6.4.5
The core rules are compact:
- maketrans can pair characters positionally: the nth character in the first string maps to the nth character in the second. section 6.4.5
- A translation table can also remove selected characters by supplying them as the third argument. section 6.4.5
- Because strings are immutable, translate produces a modified copy rather than changing the original string. section 6.4.5
- Create a reusable table outside a processing loop; rebuilding it for every line adds unnecessary work. section 6.10.2
Translate operators or remove punctuation
table = "~x ^ (y % z)".maketrans("~^()", "!&[]") "~x ^ (y % z)".translate(table) # '!x & [y % z]' remove = str.maketrans("", "", "()~^%") "~x ^ (y % z)".translate(remove) # 'x y z' section 6.4.5
Normalize text before word processing
Lowercase a line, remove punctuation in one operation, split it into words, and write one word per line. This is more compact than repeatedly calling replace for each punctuation character. section 6.10.2 section 21.2.2
import string #1
punct = str.maketrans("", "", string.punctuation) #2
with open("moby_01.txt") as infile:
with open("moby_01_clean.txt", "w") as outfile:
for line in infile:
# make all one case
cleaned_line = line.lower()
# remove punctuation
cleaned_line = cleaned_line.translate(punct) #3
# split into words
words = cleaned_line.split()
cleaned_words = "\n".join(words) #4
# write all words for line
outfile.write(cleaned_words)
Follow the complete file-cleaning pipeline using a punctuation-removal table reused for every input line. section 6.10.2

