Python计算文件中单词个数
来源:互联网 发布:c语言编程小程序 编辑:程序博客网 时间:2024/05/16 15:38
功能描述:
在linux命令行运行Python程序WordCount
1.如果只输入单个文件无命令则顺序输出此文件中单词个数、文本行数、字节数。
2.如果输入多个文件无命令则顺序输出这两个文件中单词个数、文本行数、字节数,并且计算每一列的总和total。
3.如1,2所示那样,只不过如果输入带有参数命令的则根据参数命令分别打印相应的文本,命令-w打印单词个数,-l打印文本行数,-c打印文本字节数。
代码如下:
程序名称为WordCount的缩写,wc,不要理解错误。
def wc(): import sys wcount = 0 lcount = 0 bcount = 0 wtotal = 0 ltotal = 0 btotal = 0 command = sys.argv[1:] print(command) for i in sys.argv[1:]: i = str(i) #print(command) if ".txt" in i or ".doc" in i: try: file = open(i,'r') text = file.read() file.close() wcount = len(text.split()) lcount = len(text.split("\n")) bcount = len(text) wtotal = wtotal+wcount ltotal = ltotal+lcount btotal = btotal+bcount if len(sys.argv[0:])< 100: if "-c" in command: #print("-c") if "-l" in command: #print("-l") if "-w" in command: #print("-w") print(wcount,lcount,bcount,i) continue else: print(lcount,bcount,i) continue else: print (bcount,i) continue elif "-l" not in command and "-w" not in command and "-c" not in command: print(wcount,lcount,bcount,i) continue if "-w" in command: #print("-w") if "-l" in command: #print("-l") if "-c" in command: #print("-c") print(wcount,lcount,bcount,i) continue else: print(lcount,wcount,i) continue else: print(wcount,i) continue elif "-l" not in command and "-w" not in command and "-c" not in command: print(wcount,lcount,bcount,i) continue if "-l" in command: #print("-l") if "-w" in command: #print("-w") if "-c" in command: #print("-c") print(wcount,lcount,bcount,i) continue else: print(lcount,wcount,i) continue else: print(lcount) continue elif "-l" not in command and "-w" not in command and "-c" not in command: print(wcount,lcount,bcount,i) continue #print(wtotal,ltotal,btotal,"total") except IOError: print('wrong file') if "-c" in command and "-l" in command and "-w" in command and (wtotal != wcount or ltotal != lcount or btotal != bcount ): print(wtotal, ltotal, btotal, "total") elif "-c" in command and "-l" in command and "-w" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) : print(ltotal,btotal,"total") elif "-c" in command and "-w" in command and "-l" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) : print(wtotal,btotal,"total") elif "-l" in command and "-w" in command and "-c" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) : print(wtotal,ltotal,"total") elif "-l" in command and "-w" not in command and "-c" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) : print(ltotal,"total") elif "-l" not in command and "-w" in command and "-c" not in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) : print(wtotal,"total") elif "-l" not in command and "-w" not in command and "-c" in command and (wtotal != wcount or ltotal != lcount or btotal != bcount) : print(bcount,"total") elif wtotal != wcount or ltotal != lcount or btotal != bcount : print(wtotal, ltotal, btotal, "total")wc()
在命令行格式化输入的格式如下:
多个文件:
单个文件:
其中,1.txt , 2.txt , 3.txt 是与.py文件相同路径下的3个文件,测验所用。
1.txt的内容为
one
2.txt的内容为
onetwo
3.txt的内容为
one
two
three
代码简单易懂,注释就没写。
阅读全文
1 0
- Python计算文件中单词个数
- Python使用argparse命令行选项计算文件中单词个数
- JAVA实现 计算txt文件中特定单词个数
- 计算字符串中单词个数
- 计算一条英文句子中单词个数
- Java中计算相同单词的个数
- 计算txt文本中单词的个数
- 计算一段字符串中单词的个数
- 用户从控制台输入文章、计算文章中单词个数并存入文件中
- 计算多个文件夹中,总文件个数(python)
- python统计文本中单词个数
- Python--处理文献中单词,统计个数
- 计算单词个数
- 计算单词的个数
- 计算单词个数!
- 130.计算单词个数
- 计算单词个数
- 计算单词个数
- include <> "" different
- 观察者模式与事件驱动模式实例比较
- Spring MVC中使用 hibernate-validtor进行服务端验证
- Easyui面板的left,top属性,设置当前面板的position
- shell脚本的日常使用
- Python计算文件中单词个数
- springBoot微服务框架pom.xml内容(支持jsp)
- CF 244 C. The Brand New Function 思维(二进制,区间或运算不同结果)
- 移植QT操作系统
- NOIP2011计算系数详解
- throws 和 try-catch的区别
- 【面向JS--分支结构、循环结构】
- qt5编译boost
- mysql索引