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 求10个数的平均数实例

一,已知十个数,求平均数。 L=[1,2,3,4,5,6,7,8,9,10] a=sum(L)/len(L) print("avge is:", round(a,3) ) 运行结果...

python计算最小优先级队列代码分享

复制代码 代码如下:# -*- coding: utf-8 -*- class Heap(object):     @classmethod &n...

python的urllib模块显示下载进度示例

复制代码 代码如下: def report_hook(count, block_size, total_size):...     pr...

python中字符串前面加r的作用

本文实例讲述了python中字符串前面加r的作用。分享给大家供大家参考。具体分析如下: 字符串前面加r,表示的意思是禁止字符串转义 >>> print "asfd...

python 创建一维的0向量实例

第一种方法: A=[0]*8 第二种方法: import numpy as np A=np.zeros(8) 以上这篇python 创建一维的0向量实例就是小编分享给大家...