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命名空间(namespace)简单介绍

python命名空间(namespace)简单介绍

命名空间: 每一个作用域变量存储的位置,或者解释为 存储作用域中变量的字典。 作用: 获取想查看某个作用域中的变量名、变量值。 使用方法: locals() #当前命名空间 1. 效果图...

python使用udp实现聊天器功能

python使用udp实现聊天器功能

聊天器简易版 使用udp实现一个简单的聊天器程序,要求如下: •在一个电脑中编写1个程序,有2个功能 •1.获取键盘数据,并将其发送给对方 •2.接收...

python读写ini文件示例(python读写文件)

很类似java的properties文件xml文件复制代码 代码如下:db_config.ini[baseconf]host=127.0.0.1port=3306user=rootpas...

python2.7实现邮件发送功能

python2.7实现邮件发送功能

要想实现一个能够发送带有文本、图片、附件的python程序,首先要熟悉两大模块: email以及smtplib 然后对于MIME(邮件扩展)要有一定认知,因为有了扩展才能发送附件以及...

python os.fork() 循环输出方法

先看下面这段代码: import os def main(): for i in range(0, 2): os.fork() print 'Hello'...