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中实现数组和列表读取一列的方法

在python中,普通的列表list和numpy中的数组array是不一样的,最大的不同是:一个列表中可以存放不同类型的数据,包括int、float和str,甚至布尔型;而一个数组中存放...

Android分包MultiDex策略详解

1.分包背景 这里首先介绍下MultiDex的产生背景。 当Android系统安装一个应用的时候,有一步是对Dex进行优化,这个过程有一个专门的工具来处理,叫DexOpt。DexOpt的...

Python中文编码那些事

首先,要明白encode()和decode()的区别  encode()的作用是将Unicode编码的字符串转换为其他编码格式。 例如: st1.encode("utf-8")...

django Serializer序列化使用方法详解

Serializer序列化器 定义Serializer 1. 定义方法 Django REST framework中的Serializer使用类来定义,须继承自rest_frame...

python使用RNN实现文本分类

python使用RNN实现文本分类

本文实例为大家分享了使用RNN进行文本分类,python代码实现,供大家参考,具体内容如下 1、本博客项目由来是oxford 的nlp 深度学习课程第三周作业,作业要求使用LSTM进行...