发布网友 发布时间:2024-10-14 00:01
共1个回答
热心网友 时间:2024-10-14 02:48
#!bin/python
#-*- encoding: utf-8 -*-
def counter(path, find, punctuation):
infile = open(path, "r")
lenth = len(find)
count = []
for i in range(lenth):
count.append(0)
dat = infile.readline().strip("\n")
while dat != '':
dat = dat.split()
for elemt in dat:
elemt = elemt.strip(punctuation) #去除标点符号
if elemt in find:
i = find.index(elemt)
count[i] += 1
dat = infile.readline().strip("\n")
infile.close()
for i in range(lenth):
print "%s:%d次" % (find[i],count[i])
if __name__ == "__main__":
path = "PATH"
find = ["hello", "hi", "world"]
punctuation = ''',.;'":!?'''
counter(path, find, punctuation)