分析单个文本

split()方法,是以空格为分隔符将字符串拆分成多个部分,并将这些部分存储到一个列表中

title = 'My name is oliver!'
list = title.split()
print(list)

运行结果如下:

现在存在一个文本如下:

我们要统计这个文本中有多少个字符

file_path = "txt\MyFavoriteFruit.txt"

try:
with open(file_path) as file_object:
contents = file_object.read()
except FileNotFoundError:
msg = "Sorry,the file does not exist."
print(msg)
else:
#计算该文件包含多少个单词
words = contents.split()
num_words = len(words)
print("The file "+" has about " + str(num_words) +" words.")

分析多个文本

上面只是对单个文本进行分析,那么我们对多个文本进行分析时,不可能每次都去修改file_path,所以在这里我们使用函数来进行分析

def count_words(file_path):
try:
with open(file_path) as file_object:
contents = file_object.read()
except FileNotFoundError:
msg = "Sorry,the file does not exist."
print(msg)
else:
#计算该文件包含多少个单词
words = contents.split()
num_words = len(words)
print("The file "+" has about " + str(num_words) +" words.") #调用函数
file_path="txt\MyFavoriteFruit.txt"
count_words(file_path)

加入现在想对A.txt,B.txt,C.txt三个文件同时统计文件字数,那么只需要循环调用即可

def count_words(file_path):
try:
with open(file_path) as file_object:
contents = file_object.read()
except FileNotFoundError:
msg = "Sorry,the file does not exist."
print(msg)
else:
#计算该文件包含多少个单词
words = contents.split()
num_words = len(words)
print("The file "+" has about " + str(num_words) +" words.") #调用函数
file_paths = ['txt\A.txt','txt\B.txt','txt\C.txt']
for file_path in file_paths:
count_words(file_path)

运行结果:

【Python】分析文本split()的更多相关文章

  1. Python进行文本处理

    对于一个文本字符串,可以使用Python的string.split()方法将其切割.下面看看实际运行效果. mySent = 'This book is the best book on python ...

  2. python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  3. 用Python分析国庆旅游景点,告诉你哪些地方好玩、便宜、人又少

    注:本人参考“裸睡的猪”公众号同名文章,学习使用. 一.目标 使用Python分析出国庆哪些旅游景点:好玩.便宜.人还少的地方,不然拍照都要抢着拍! 二.获取数据 爬取出行网站的旅游景点售票数据,反映 ...

  4. python 分析慢查询日志生成报告

    python分析Mysql慢查询.通过Python调用开源分析工具pt-query-digest生成json结果,Python脚本解析json生成html报告. #!/usr/bin/env pyth ...

  5. 五月天的线上演唱会你看了吗?用Python分析网友对这场线上演唱会的看法

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:CDA数据分析师 豆瓣9.4分!这场线上演唱会到底多好看? 首先让我 ...

  6. python join与split函数的用法举例

    python join 和 split方法: join用来连接字符串,split恰好相反,拆分字符串的. 来看有关join.split方法的例子 1,join用法的例子 复制代码 代码示例: > ...

  7. python join和split和strip用法

    python join 和 split方法的使用,join用来连接字符串,split恰好相反,拆分字符串的. strip()为去除开头结尾指定的字符,空着时是去除空白字符\t,\n,\r意思 1.jo ...

  8. 举例详解Python中的split()函数的使用方法

    这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下   函数:sp ...

  9. python join 和 split的常用使用方法

    函数:string.join()Python中有join()和os.path.join()两个函数,具体作用如下:    join():    连接字符串数组.将字符串.元组.列表中的元素以指定的字符 ...

随机推荐

  1. 【bzoj3555】[Ctsc2014]企鹅QQ 字符串hash

    题目描述 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体 ...

  2. 使TileCache配合OpenLayers,产生地图瓦块的一些资料(转)

    在tilecache.cfg中配置好被切割地图的参数,比如: [mytestmap]layers=3,5,7,8type=WMSurl=http://localhost/arcgis/services ...

  3. DocumentFragment批量操作dom

    DocumentFragment,文档片段,不属于文档树,其parentNode为null.当把一个DocumentFragment节点插入文档树时,插入的不是DocumentFragment自身,而 ...

  4. 使用libcurl提示 LNK2001的错误

    vs使用libcurl(static library),link时报错: error LNK2001: unresolved external symbol __imp__curl_easy_perf ...

  5. element ui form表单清空规则

    公司项目重构,经过商定使用element ui.在重构项目的时候发现一下element ui上很蛋疼的东西. 例如,这个form表单就是一个.趁着在高铁上没事,把想写的东西写一下. 先说一下eleme ...

  6. pat 团体天梯赛 L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  7. 用c#语言通过修改注册表改IE网页首页

    原文发布时间为:2009-04-19 -- 来源于本人的百度文章 [由搬家工具导入] string key = @"HKEY_CURRENT_USER\Software\Microsoft\ ...

  8. Hadoop-hdfs安装与配置

    一.安装要求   安装JDK   yum -y install jdk(或手动安装)  设置namenode节点到datanode节点的免密码登陆   a. 本地免密码登录     # ssh loc ...

  9. html5---音频视频基础一

    //html5 音频和视频 :标签 a: audio,video b: source :视频容器 a:容器文件,类似于压缩了一组文件 -音频轨道 -视频轨道 -元数据:封面,标题,字幕等 -格式:.a ...

  10. 机器学习3_EM算法与混合高斯模型

    ①EM算法: http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006936.html 李航 <统计学习方法>9.1节 ②混合高斯模 ...