python怎么统计txt文件的字数

互联网 19-7-6

Python中要统计txt文件字数可以rstrip函数和len函数进行。

如下统计白夜行.txt的字数:

file_name = '白夜行.txt' try:     with open(file_name, encoding='utf8') as file_obj:     #由于书名不是英文,要加上 encoding='utf8'         contents = file_obj.read() except FileNotFoundError:     msg = 'Sorry, the file' + file_name + ' does not exist.'     print(msg) else:     words = contents.rstrip()     num_words = len(words)     print('The file ' + file_name + ' has about ' + str(num_words) + ' words.')
The file 白夜行.txt has about 487761 words.

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上就是python怎么统计txt文件的字数的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:python转大数据容易吗

相关资讯