我是python新手,我试图打印文本文件中的总字数和用户提供的文件中的特定字数。
我测试了我的代码,但是结果输出了单个单词,但是我只需要文件中所有单词的总单词数,以及用户提供的单词的总单词数。
代码:
name = raw_input("Enter the query x ")
name1 = raw_input("Enter the query y ")
file=open("xmlfil.xml","r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in wordcount.items():
print k, v
for name in file.read().split():
if name not in wordcount:
wordcount[name] = 1
else:
wordcount[name] += 1
for k,v in wordcount.items():
print k, v
for name1 in file.read().split():
if name1 not in wordcount:
wordcount[name1] = 1
else:
wordcount[name1] += 1
for k,v in wordcount.items():
print k, v
样本输出
请不要命名要处理
open()
结果file
的变量,否则将覆盖file
类型的构造函数。您可以通过
Counter
轻松获得所需内容相关问题 更多 >
编程相关推荐