分析单个文本

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. Log4j官方文档翻译(一、基本介绍)

    简介 log4j是使用java语言编写的可靠的.快速的.灵活的日志框架,它是基于Apache的license. log4j支持c,c++,c#,perl,python,ruby等语言.在运行时通过额外 ...

  2. java连接mysql数据库 三 实现增删改查操作

    同以前一样,先写一个数据库打开和关闭操作类 public class DBConnection { String driver = "com.mysql.jdbc.Driver"; ...

  3. hihoCoder 第136周 优化延迟(二分答案+手写堆)

    题目1 : 优化延迟 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho编写了一个处理数据包的程序.程序的输入是一个包含N个数据包的序列.每个数据包根据其重要程度不同 ...

  4. inline-block元素垂直对齐

    多个inline-block元素使用vertical-align:middle无法对齐,必须有个height:100%的子元素才行,通常使用伪元素.另一种方法是添加line-height:normal ...

  5. ci支持pathinfo

    location ~ \.php { #去掉$ root H:/PHPServer/WWW; fastcgi_pass ; fastcgi_index index.php; fastcgi_split ...

  6. C# 自动注册OCX方法

    C#开发系统时,有时候会遇到调用其他语言开发的模块.如果对方提供了OCX时,就需要注册使用,但是实时时,每个客户端都注册一遍就比较麻烦.所以需要系统第一次启动时自动注册OCX. ​ 一:C#注册OCX ...

  7. Mysql和sqlServer命令比较

    http://cool.china.blog.163.com/blog/static/697310642010111202531210 Mysql和sqlServer命令比较 按语句功能划分,依次讲解 ...

  8. python面试题目【转1】

    原文地址:https://www.usblog.cc/blog/post/justzhl/b5cc9a05c7d2 问题一:以下的代码的输出将是什么? 说出你的答案并解释. 1 2 3 4 5 6 7 ...

  9. Java之Jenkins工具【转】

    1.1 前言 Jenkins是一个用Java编写的开源的持续集成工具.在与Oracle发生争执后,项目从Hudson项目独立. Jenkins提供了软件开发的持续集成服务.它运行在Servlet容器中 ...

  10. form快速转json serialize

    原文发布时间为:2011-03-28 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...