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连接mysql数据库示例(做增删改操作)

一、相关代码数据库配置类 MysqlDBConn.py 复制代码 代码如下:#encoding=utf-8'''Created on 2012-11-12Mysql Conn连接类'''...

python实现mysql的读写分离及负载均衡

python实现mysql的读写分离及负载均衡

Oracle数据库有其公司开发的配套rac来实现负载均衡,目前已知的最大节点数能到128个,但是其带来的维护成本无疑是很高的,并且rac的稳定性也并不是特别理想,尤其是节点很多的时候。...

python 顺时针打印矩阵的超简洁代码

如下所示: # -*- coding:utf-8 -*- class Solution: # matrix类型为二维列表,需要返回列表 def printMatrix(sel...

Python中的集合介绍

Python中的集合介绍

1.集合的定义 集合的元素是不可重复的 s = {1,2,3,1,2,3,4,5} print(s) print(type(s)) s1 = {1} print(s1) print(...

python搜索包的路径的实现方法

查看python搜索包的路径的实现方法: python搜索包的路径存储在sys.path下 查看方法: import sys sys.path 临时添加python搜索包路径的方法: 方...