您的位置:首页 > 编程语言 > Python开发

【Python 写的小工具】计算 txt 文件中中文字符的平均字数和平均行数

2018-03-30 00:26 741 查看
还是老师让写的小程序,好像是用来处理文字识别之后产生的 txt 文件。
怎么用:传入一个目录,还你一个result.txt。
具体看代码吧,写的很直白。import os

def is_chinese(uchar):
if uchar >= u'\u4E00' and uchar <= u'\u9FA5':
return True
else:
return False

def count_word(file):
f=open(file, 'r', encoding='UTF-8')
count=0
for i in f.read():
if is_chinese(i):
count+=1
f.close()
print(file)
return count

def count_line(file):
f1=open(file, 'r+', encoding='UTF-8')
temp=[]
for line in f1.readlines():
if line != '\n':
temp.append(line)
f1.close()
f2=open(file, 'r+', encoding='UTF-8')
f2.truncate()
for line in temp:
f2.write(line)
f2.close()
f3=open(file, 'r+', encoding='UTF-8')
lines=len(f3.readlines())
f3.close()
return lines

def average(dir):
list=os.listdir(dir)
os.chdir(dir)
sumword=0
sumline=0
for i in list:
sumword=sumword+count_word(i)
sumline=sumline+count_line(i)
os.chdir("..")
f=open("result.txt", "w+", encoding='UTF-8')
f.write("%s %s %.3f\n" %(dir ,"目录中平均汉字字数为:" ,sumword/len(list)))
f.write("%s %s %.3f\n" %(dir ,"目录中平均行数为:" ,sumline/len(list)))
f.close

average("t")为什么里面数行数的那个函数那么麻烦,是因为老师给的需求是要忽略txt文件里的空白行,不仅忽略,在程序运行的时候,直接把有空白行的 txt 文件里的空白行去掉了。如果不需要去掉,程序会简单很多。半个月前写的了,也忘了,感觉挺浅显的,就不写注释了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: