import sys;
import string;
def SearchWord(strFile, strWord):
print("Search file: ", strFile, " word:", strWord);
nCount = 0;
nLine = 0;
try:
hFile = open(strFile, "r");
try:
while True:
strLine = hFile.readline();
if strLine:
#print(strLine);
nLineCount = FindStrCount(strLine, strWord);
nLine = nLine + 1;
print("line ", nLine, " count:", nLineCount);
nCount = nCount + nLineCount;
else:
break;
finally:
hFile.close();
except IOError:
print("open ", strFile , " failed!");
return nCount;
def FindStrCount(strLine, strWord):
nFind = 0;
nIndex = 0;
while True:
nIndex = strLine.find(strWord, nIndex);
if nIndex < 0:
break;
nFind = nFind + 1;
nIndex = nIndex + 1;
return nFind;
if __name__ == "__main__":
#test for find sub string numbers
#str = "aa bb cc dd ee ee bb aa aa dd";
#print(FindStrCount(str, "bb"));
#test for count words
#print(SearchWord("c:\\c.txt", "=>"));
#sys.exit();
if 1 == len(sys.argv) or sys.argv[1] in ("-h", "--help"):
print("usage: wc.py word file1 file2 ... filen");
sys.exit();
#count word
ls = [];
i = 0;
for i in range(0, len(sys.argv)):
if i > 1:
ls.append(sys.argv[i]);
elif 1 == i:
strWord = sys.argv[i];
else:
print("work py file:", sys.argv[i]);
print("search word:", strWord);
for strFile in ls:
print("parse file:", strFile);
print("find ", strWord, " count:", SearchWord(strFile, strWord));
分享到:
相关推荐
读取给定文本文件,统计单词,计算TOP 10 有一个文件sample.txt,对其进行单词统计,不区分大小写,并显示单词重复最多的10个单词。
统计所需搜索的文件的每个单词的数量和单词的名字,直观反映在桌面
用二叉搜索树实现统计一个文件中单词的个数
统计文章中单词的个数,并且输出最多的15个单词
用python统计词频脚本,nltk使用等。。。
利用python语言统计一篇文档中汉字的个数
编写一个程序,分析一个文本文件(英文...文本文件为D:\test.txt ,输出该文本文件中含有的单词总数,分析一个文本文件(英文文章)中 各个词出现的频率,输出该文本文件中所有单词的列表,并与出现次数一起打印出来
该程序用于统计一个字符串中中英文、空格、数字和标点符号的个数。
一种寻找曲线峰值并统计峰的个数的python代码.pdf
编写程序统计一组字符串中,单词(用空格隔开的连续的字符)的个数,大写字母的个数,小写字母的个数,数字的个数,空格的个数和其他字符的个数,调用函数实现
你可以轻松统计你输入的正负数的个数,而且不限制输入的个数,以零结束输入。。。
主要介绍了Python实现统计单词出现的个数,本文给出了实现代码以及使用方法,需要的朋友可以参考下
统计所有文本文档里各种字符的个数,里面有源码
POINTERS ON C 第九章编程练习11统计某个单词出现的个数,示例为单词the。VC6.0编译通过
用数组方法实现从文件中读取单词并统计单词个数
统计一篇文章中每个单词的个数 小程序 源代码 很好用。 界面清秀柔和。
统计C程序单词的个数 ——Hash技术 数据结构”是计算机程序设计的重要理论技术基础,本次数据结构课程设计的内容主要是考察数据结构中的查找,查找是数据结构中很重要的一章,其实在日常生活中我们,我们几乎每天都...
程序读取一个英文文本文件,使用者输入一个英文单词,它就会统计该英文单词在这段英文中出现的次数及出现的位置。
这是C++程序,能够统计文件中字母的个数,且占用资源少,程序简单明了.
//num 用来统计单词的个数 //state 用来记录程序当前是否处于一个单词之中,初值为0,表示不在单词中,值为1,表示正处于在一个单词中 printf("Please input the number of lines for English passage:"); scanf...