Python计算一个文件里字数的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python计算一个文件里字数的方法。分享给大家供大家参考。具体如下:

这段程序从所给文件中找出字数来。

from string import *
def countWords(s):
  words=split(s)
  return len(words)
  #returns the number of words
filename=open("welcome.txt",'r')
#open an file in reading mode
total_words=0
for line in filename:
  total_words=total_words + countWords(line)
  #counts the total words
print total_words

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python进程间通信 multiProcessing Queue队列实现详解

一、进程间通信 IPC(Inter-Process Communication) IPC机制:实现进程之间通讯 管道:pipe 基于共享的内存空间 队列:pipe+锁的概念--->...

解决python 文本过滤和清理问题

问题 某些无聊的脚本小子在Web页面表单中填入了“pýtĥöñ”这样的文本,我们想以某种方式将其清理掉。 解决方案 文本过滤和清理所涵盖的...

python3中dict(字典)的使用方法示例

一、clear(清空字典内容) stu = { 'num1':'Tom', 'num2':'Lucy', 'num3':'Sam', } print(stu.clear...

Django中多种重定向方法使用详解

前言 本文使用了Django1.8.2 使用场景,例如在表单一中提交数据后,需要返回到另一个指定的页面即可使用重定向方法 一、 使用HttpResponseRedirect fu...

python实现多进程按序号批量修改文件名的方法示例

本文实例讲述了python实现多进程按序号批量修改文件名的方法。分享给大家供大家参考,具体如下: 说明 文件名命名方式如图,是数字序号开头,但是中间有些文件删掉了,序号不连续,这里将序号...