Skip to content

Instantly share code, notes, and snippets.

@savanovich
Last active April 16, 2016 01:56
Show Gist options
  • Save savanovich/f12b80ceb33ad42f554f2c9cfd2eb626 to your computer and use it in GitHub Desktop.
Save savanovich/f12b80ceb33ad42f554f2c9cfd2eb626 to your computer and use it in GitHub Desktop.
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']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment