Last active
April 16, 2016 01:56
-
-
Save savanovich/f12b80ceb33ad42f554f2c9cfd2eb626 to your computer and use it in GitHub Desktop.
String translation table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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