0
点赞
收藏
分享

微信扫一扫

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


1,Windows+R启动:运行

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

2,输入:cmd 回车打开!

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

3,输入python(假设你已经安装过了python)

里面还会出现版本号!

python 获取你电脑纯文本文档内容!解决IndentationError: expected an indented block报错!_不区分大小写_03

4,输入代码:

注意:如果空格缩进不对,会报错的!

比如:

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

下面是正确的代码!

  #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报错!_python_05


5,OK!动手试试吧,有问题留言哈。看到就回复!


举报

相关推荐

0 条评论