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中for循环的使用方法

详解Python中for循环的使用方法

 for循环在Python中有遍历所有序列的项目,如列表或一个字符串。 语法: for循环语法如下: for iterating_var in sequence: st...

python发送HTTP请求的方法小结

本文实例讲述了python发送HTTP请求的方法。分享给大家供大家参考。具体如下: 这里包含 Python 使用 GET/HEAD/POST 方法进行 HTTP 请求 1. GET 方法...

python 捕获 shell/bash 脚本的输出结果实例

#!/usr/bin/python ## get subprocess module import subprocess   ## call date command ##...

解决python大批量读写.doc文件的问题

前言: java语言读写.doc的出现乱码问题: 大家都知道当我们利用java语言读写.doc文件时,无论是利用流的方式将.doc文件的内容输出到控制台(console),还是将其写到其...

Windows系统下PhantomJS的安装和基本用法

Windows系统下PhantomJS的安装和基本用法

1.安装 下载网址:http://phantomjs.org/download.html 选择合适的版本。然后解压即可。 环境变量的配置: 进入解压的路径: 例如我是解压在D:\Py...