Python’s string module supplies reusable character constants for string-processing tasks, especially when code needs a defined set of punctuation, digits, whitespace, or ASCII letters. section 6.4.7
Useful constants include:
- string.punctuation: punctuation characters used to build a translation table. section 6.10.2
- string.whitespace: characters Python treats as whitespace on the current system. section 6.4.7
- string.digits, string.hexdigits, and string.octdigits: decimal, hexadecimal, and octal digit sets. section 6.4.7
- string.ascii_lowercase, string.ascii_uppercase, and string.ascii_letters: lowercase, uppercase, and combined ASCII letters. section 6.4.7
Remove punctuation efficiently
Create the translation table once, then reuse it while processing lines: import string; punct = str.maketrans("", "", string.punctuation); cleaned = line.lower().translate(punct). Building the table outside the loop avoids repeating the same operation for every line. section 6.10.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)
Shows a complete file-cleaning pipeline that lowercases text, removes punctuation, splits lines into words, and writes one word per line. section 6.10.2
Import string wherever string.punctuation or another module constant is used. A generated module that omits this import raises NameError; after fixing an already loaded module, reload it or restart the session. section 10.8.2

