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实现在pickling的时候压缩的方法

本文实例讲述了python实现在pickling的时候压缩的方法。分享给大家供大家参考。 具体方法如下: import cPickle,gzip def save(filename,...

python如何把嵌套列表转变成普通列表

如何把[1, 5, 6, [2, 7, [3, [4, 5, 6]]]]变成[1, 5, 6, 2, 7, 3, 4, 5, 6]? 思考:   -- for循环每次都遍历列表一层   ...

python高效过滤出文件夹下指定文件名结尾的文件实例

如下所示: import os def anyTrue(predicate, sequence): return True in map(predicate, sequence)...

使用PYTHON解析Wireshark的PCAP文件方法

PYTHON首先要安装scapy模块 PY3的安装scapy-python3,使用PIP安装就好了,注意,PY3无法使用pyinstaller打包文件,PY2正常 PY2的安装scapy...

Python tkinter实现的图片移动碰撞动画效果【附源码下载】

Python tkinter实现的图片移动碰撞动画效果【附源码下载】

本文实例讲述了Python tkinter实现的图片移动碰撞动画效果。分享给大家供大家参考,具体如下: 先来看看运行效果: 具体代码如下: #!/usr/bin/python #...