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程序设计有所帮助。

相关文章

python3.x 生成3维随机数组实例

python3.x 生成3维随机数组实例

如下所示: import numpy as np a=np.random.randint(0,10,size=[3,3,3]) print(a) 以上这篇python...

Python简单调用MySQL存储过程并获得返回值的方法

本文实例讲述了Python调用MySQL存储过程并获得返回值的方法。分享给大家供大家参考。具体实现方法如下: try: conn = MySQLdb.connect (...

PyQT实现菜单中的复制,全选和清空的功能的方法

PyQt的文本操作的继承关系: QTextBrowser ( QtGui.QTextEdit) 其中QTextEdit具有的功能函数: copy() 复制 selectAll() 全选...

对Python 除法负数取商的取整方式详解

python除法负数商的取整方式与C++不同 python: 5 / -2 = -3 若想和C++行为相同,可以使用 int(operator.truediv(num1, num2...

Python 12306抢火车票脚本 Python京东抢手机脚本

本文实现12306抢火车票/京东抢手机示例,具体如下: #12306秒抢Python代码 from splinter.browser import Browser x = Brows...