Apr 16, 2016

String translation table

import string
# In [1]: string.punctuation
# Out[1]: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
# In [2]: string.uppercase
# Out[2]: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# In [3]: " " * len(string.punctuation) + string.lowercase
# Out[3]: ' abcdefghijklmnopqrstuvwxyz'
# translation table maps upper case to lower case and punctuation to spaces
translation_table = string.maketrans(string.punctuation+string.uppercase,
" " * len(string.punctuation) + string.lowercase)
line = ""
line = line.translate(translation_table)
# In [4]: line
# Out[4]: 'foo bar bizz'
word_list = line.split()
# In [5]: word_list
# Out[5]: ['foo', 'bar', 'bizz']