天天看點

python 擷取你電腦純文字文檔内容!解決IndentationError: expected an indented block報錯!

1,Windows+R啟動:運作

python 擷取你電腦純文字文檔内容!解決IndentationError: expected an indented block報錯!

2,輸入:cmd 回車打開!

python 擷取你電腦純文字文檔内容!解決IndentationError: expected an indented block報錯!

3,輸入python(假設你已經安裝過了python)

裡面還會出現版本号!

python 擷取你電腦純文字文檔内容!解決IndentationError: expected an indented block報錯!

4,輸入代碼:

注意:如果空格縮進不對,會報錯的!

比如:

python 擷取你電腦純文字文檔内容!解決IndentationError: expected an indented block報錯!

下面是正确的代碼!

#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")
    #路徑要正确!注意是這個“/”斜杠 不是“\”這個斜杠!      

執行結果:

python 擷取你電腦純文字文檔内容!解決IndentationError: expected an indented block報錯!