方案一:
def wordcount(file='test.txt'):
chars='''~!@#$%^&*()_+={}[]|\:;"'<,>.?/'''
charset = set(chars)
with open(file,encoding='utf-8') as f:
word_count = {}
for line in f:
words = line.split()
for k,v in zip(words,(1,)*len(words)):
k=k.strip(chars)
if len(k) <1:
continue
k = k.lower()
start = 0
for i,c in enumerate(k):
if c in charset:
if start == i:
start = i + 1
continue
key = k[start:i]
word_count[key] = word_count.get(key,0)+1
start = i+1