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

yipeiwu_com6年前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新手经常遇到的17个错误分析

1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)...

利用Anaconda简单安装scrapy框架的方法

利用Anaconda简单安装scrapy框架的方法

引言:使用pip install 来安装scrapy需要安装大量的依赖库,这里我使用了Anaconda来安装scrapy,安装时只需要一条语句:conda install scrapy即...

python笔记:mysql、redis操作方法

模块安装: 数据操作用到的模块pymysql,需要通过pip install pymysql进行安装。 redis操作用的模块是redis,需要通过pip install redis进行...

Python文件监听工具pyinotify与watchdog实例

pyinotify库 支持的监控事件 @cvar IN_ACCESS: File was accessed. @type IN_ACCESS: int @cvar IN_MODIFY...

Django框架中处理URLconf中特定的URL的方法

有时你有一个模式来处理在你的URLconf中的一系列URL,但是有时候需要特别处理其中的某个URL。 在这种情况下,要使用将URLconf中把特殊情况放在首位的线性处理方式 。 比方说,...