• 作业(二)—python实现wc命令


    Gitee地址:https://gitee.com/c1e4r/word-count(为什么老师不让我们用github)

    0x00 前言

          好久没发博客了,感觉自己的学习是有点偷懒了。这篇博客也是应专业课程老师作业硬性的要求才发的,希望借这次机会,自己能够再次出发,努力向前吧。

    0x01 作业思路

      老师在课堂上布置了一个作业,总的来说是对一个源文件进行各方面的字符统计,涉及到了文件和字符读取等操作。具体要求如下:

    其实要求很简单,主要就是文件读写操作,又必须是用命令行进行,python中自带的sys库和getopt库可以从命令行中获取参数,所以就果断用python来解决了。

    0x02 基本实现过程

      基本功能只有三个:统计源文件字符,统计源文件单词,统计源文件行数

      所以就只需要写三个功能函数加上一个主函数即可实现其功能。话不多说,直接先贴代码:

    #Author:c1e4r
    
    import sys
    import getopt
    
    #读取指定文件中的字符数
    def read_str(file2):
        file = sys.argv[-1]
        file1 = open(file,'r+')
        count = 0
        for line in file1.readlines():
            count_line = len(line)
            count += count_line
        f = open(file2,'a+')
        f.write(file+",字符数:"+str(count)+'
    ')
        file1.close()
        f.close()
    
    #读取指定文件中的单词数
    def read_word(file2):
        file = sys.argv[-1]
        file1 = open(file,'r+')
        count = 0
        for line in file1.readlines():
            line = line.replace(","," ")
            line = line.replace("."," ")
            line = line.replace("!"," ")
            line = line.replace("?"," ")
            line_word = len(line.split( ))
            count += line_word 
        f = open(file2,'a+')
        f.write(file+",单词数:"+str(count)+'
    ')
        file1.close()
        f.close()
    
    #读取指定文件中的行数
    def read_line(file2):
        file = sys.argv[-1]
        file1 = open(file,'r+')
        count = 0
        for line in file1.readlines():
            count += 1
        f = open(file2,'a+')
        f.write(file+",行数:"+str(count)+'
    ') 
        file1.close()
        f.close()
        
    def main():
        file = "result.txt"
        try:
            opts, args = getopt.getopt(sys.argv[1:],"hc:w:l:o:")
        except getopt.GetoptError:
            print("test.py [parameter] [input_file_name]")
            sys.exit(2)
        finally:
            pass
        for opt,arg in opts:
            if opt == '-h':
                print("test.py -c -w -l -o <outputfile> [input_file_name]") # -h 使用说明
                sys.exit()
            elif opt == "-c": 
                read_str(file)  # -c 返回文件的字符数
            elif opt == "-w":
                read_word(file) # -w 返回文件的单词总数
            elif opt == "-l":
                read_line(file) # -l 返回文件的总行数
            elif opt == "-o":
                file = arg      # -o 输出结果的文件   
      
    if __name__ == "__main__":
        main()

    其中稍微有点棘手的是统计文件中的单词数量。因为单词和单词之间不一定是空格(可能是各种标点符号),所以就先把每一行中的标点符号替换成空格,最后用split切割,这样每个单词就被筛选出来了

        for line in file1.readlines():
            line = line.replace(","," ")
            line = line.replace("."," ")
            line = line.replace("!"," ")
            line = line.replace("?"," ")
            line_word = len(line.split( ))

    其他的功能其实就是一个逐行读然后计算的过程,很基础,所以就不做解析了。

    0x04 效果截图

    程序的效果如下:

     后面还要求做个图形化界面。。。我还是老老实实加油学安全吧。

    参考文献:   Python命令行参数和getopt模块

  • 相关阅读:
    Delphi关于记录文件的操作转
    数字电视分辨率
    delphi FileSetAttr 设置文件的属性转
    vc delphi 回调函数具体说明和实例与分析 转
    TFileStream(文件流) 读写转
    HDMI接口
    UDP和TCP协议包大小的计算转
    字符编解码的故事(ASCII,ANSI,Unicode,Utf8) 转
    [bzoj3894]文理分科
    [bzoj5338]xor
  • 原文地址:https://www.cnblogs.com/c1e4r/p/9696773.html
Copyright © 2020-2023  润新知