Open StevenCHowell opened 7 years ago
To replace punctuation with a space use the following:
transtable = str.maketrans(string.punctuation, ' '*len(string.punctuation))
import string
def main():
transtable = str.maketrans(string.punctuation, ' '*len(string.punctuation))
with open('alice30.txt') as data:
text = data.read().replace("'", "").translate(transtable).lower()
wordList = text.split()
count = {}
for w in wordList:
count[w] = count.get(w, 0) + 1
keyList = sorted(count.keys())
for k in keyList:
print("%-20s occurred %4d times"%(k, count[k]))
main()
This example demonstrates a slightly more consolidated method for removing punctuation.