分析单个文本

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. P2165 [AHOI2009]飞行棋

    题目描述 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. 输入输出格式 输入格式: 第一行为 ...

  2. Codeforces 1038F Wrap Around (Codeforces Round #508 (Div. 2) F) 题解

    写在前面 此题数据量居然才出到\(n=40\)???(黑人问号)以下给出一个串长\(|S|=100,n=10^9\)的题解. 题目描述 给一个长度不超过\(m\)的01串S,问有多少个长度不超过\(n ...

  3. 学习RMQ-ST表

    RMQ一般是一个二维数组,用$dp[i][j]$表示起点为i开始连续数$2^j$个元素其中的最值,由于对于一个闭区间有:$R-L+1=len$因此也可以用另外一种记法:闭区间为$[i,i+2^j-1] ...

  4. BZOJ3166 [Heoi2013]Alo 【可持久化trie树 + 二分 + ST表】

    题目 Welcome to ALO ( Arithmetic and Logistic Online).这是一个VR MMORPG , 如名字所见,到处充满了数学的谜题. 现在你拥有n颗宝石,每颗宝石 ...

  5. linux系统——etc下的passwd 文件

    etcpasswd 文件 在登陆时要求输入用户名和密码,就是根据这个来的. root::0:0:root:/root:/bin/bash bin:x:1:1:bin:/dev/null:/bin/fa ...

  6. Python之面向对象:属性

    一.属性定义 1.类属性 类属性定义在类中且在函数体之外:类属性通常不作为实例属性使用:类变量紧接在类名后面定义 类属性的引用:类名.count eg:Employee.count 实例中可以引用类的 ...

  7. Javascript的SEO优化技巧

    原文发布时间为:2010-10-22 -- 来源于本人的百度文章 [由搬家工具导入] 1.外部崁入javascript在撰写一些比较复杂的网页特效,如下拉式选单等,会产生大量的javascript码, ...

  8. 写文章 TEE技术分析【转】

    转自:https://zhuanlan.zhihu.com/p/24222064 首先介绍一下TEE的主要关键技术: 1.安全启动(Secure Boot) 安全启动技术可以用于需要防止篡改系统镜像, ...

  9. TDictionary字典 记录 的赋值。

    type TRen = record age: Integer; //把name定义成结构的属性. private Fname: string; procedure Setname(const Val ...

  10. 阿里云服务器 centos 7 安装postgresql 11

    Postgresql简介 官方网站:https://www.postgresql.org/ 简介参考zhihu文章 https://www.zhihu.com/question/20010554 关于 ...