任一个英文的纯文本文件,统计其中的单词出现的个数。

College will be the most important years in your life. It is in college that you will truly discover what learning is about. You often question "what good is this course". I encourage you to be inquisitive, but I also want to tell you: "Education is what you have left after all that is taught is forgotten." What I mean by that is the materials taught isn't as important as you gaining the ability to learn a new subject, and the ability to analyze a new problem. That is really what learning in college is about – this will be the period where you go from teacher-taught to master-inspired, after which you must become self-learner. So do take each subject seriously, and even if what you learn isn't critical for your life, the skills of learning will be something you cherish forever.

问题简短明确,输入是一个满是英文纯*文本文件*,输出就是每个单词出现的*次数*。
很明显,用dict可以解决这个问题,但是要写的简短而又严谨还是需要琢磨一下。
读取文件是应当使用try语句块来包裹的,防止文件读取错误,而Python里面的with...as方式恰恰就是更简单的方法,其实效果和try...catch一样。
其次就是标点符号的去除,正常想法应当是便利字符串的每个字符,然后替换掉标点符号。恰好Python有一个str.translate()的方法可以将字符串的中指定的字符替换成其他的字符,而string.punctuation则提供了包含标点符号的字符串。

__author__ = 'zynick'
import string
total = 0
dic = {}
remove_list = str.maketrans('','',string.punctuation)
with  open('text.txt','r',encoding='utf8') as file:
    for line in file:
        line = line.translate(remove_list)
        list = line.split()
        total += len(list)
        for word in list:
            if word in dic:
                dic[word] += 1
            else:
                dic[word] = 1
print("total:"+str(total)+"\n")
for k,v in dic.items():
    print(k,v)

输出如下:

total:142

each 1
really 1
gaining 1
about 2
seriously 1
want 1
masterinspired 1
all 1
to 5
isnt 2
something 1
even 1
It 1
So 1
That 1
period 1
mean 1
important 2
also 1
life 2
good 1
subject 2
that 3
of 1
College 1
ability 2
left 1
what 5
years 1
skills 1
where 1
become 1
truly 1
teachertaught 1
forever 1
be 4
will 4
if 1
do 1
materials 1
take 1
course 1
the 6
most 1
for 1
after 2
problem 1
which 1
I 3
cherish 1
inquisitive 1
critical 1
– 1
as 2
in 3
college 2
new 2
analyze 1
your 2
must 1
taught 2
learning 3
is 9
selflearner 1
discover 1
You 1
What 1
a 2
question 1
encourage 1
this 2
Education 1
by 1
have 1
from 1
often 1
go 1
you 9
but 1
forgotten 1
learn 2
tell 1
and 2