from operator import itemgetter
file_name = "F:\\python.txt"
line_counts = 0
word_counts = 0
character_counts = 0
count = {}
word = ""
with open(file_name, 'r') as f:
for line in f:
words = line.split()
for index,word in enumerate(words):
if word in count:
count[word] += 1
else:
count[word] = 1
line_counts += 1
word_counts += len(words)
character_counts += len(line)
for index, item in count.items():
print("单词",index,"个数=",item)
print("行数 ", line_counts)
print("单词的个数 ", word_counts)
print("字节数 ", character_counts)
转载于:https://my.oschina.net/u/238361/blog/359944