python 获取你电脑纯文本文档内容!解决IndentationError: expected an indented block报错!

1,Windows+R启动:运行
在这里插入图片描述2,输入:cmd 回车打开!
加粗样式
3,输入python(假设你已经安装过了python)
里面还会出现版本号!
在这里插入图片描述4,输入代码:
注意:如果空格缩进不对,会报错的!
比如:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181105093942717.png

下面是正确的代码!

  #coding=utf-8 #编码方式!
    import re 
    def get_word_frequencies(file_name): 
        dic = {} #英文模式下4个空格!
        txt = open(file_name, 'r').read().splitlines() #英文模式下4个空格!
        n=0 #英文模式下4个空格!
        for line in txt:#英文模式下4个空格!
            print line #英文模式下8个空格!
            line = re.sub(r'[.?!,""/]', ' ', line) #英文模式下8个空格! 要替换的标点符号,英文字符可能出现的 
            line = re.sub(r' - ', ' ', line) #英文模式下8个空格! 替换单独的‘-’ 
            for word in line.split(): #英文模式下8个空格! 当一行的最后一个字符是-的时候,需要跟下一个英文字符串联起来构成单词 
                if word[-1] =='-': #英文模式下12个空格!
                    m=word[:-1] #英文模式下16个空格!
                    n=1 #英文模式下16个空格!
                break #英文模式下8个空格!
        if n==1: #英文模式下4个空格!
            word=m+word #英文模式下8个空格!
            n=0 #英文模式下8个空格!
        print word #英文模式下4个空格!
        dic.setdefault(word.lower(), 0) # 英文模式下4个空格!不区分大小写  
        dic[word.lower()] += 1  #英文模式下4个空格!print dic  
    
    get_word_frequencies("C:/Users/SHAOWENQIANG/Desktop/x07bc.txt")
    #路径要正确!注意是这个“/”斜杠 不是“\”这个斜杠!

执行结果:
在这里插入图片描述
5,OK!动手试试吧,有问题留言哈。看到就回复!

猜你喜欢

转载自blog.csdn.net/weixin_42859280/article/details/83743830
今日推荐