Python实现统计英文单词个数及字符串分割代码

yipeiwu_com6年前Python基础

字符串分割

复制代码 代码如下:

str="a|and|hello|||ab"
alist = str.split('|')
print alist

结果


复制代码 代码如下:

str="a hello{这里换成5个空格}world{这里换成3个空格}"
alist=str.split(' ')
print alist

统计英文单词的个数的python代码

复制代码 代码如下:

# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #获取当前文件名称
fin = open(u'c:/a.txt')

info = fin.read()
alist = info.split(' ') # 将文章按照空格划分开

fout = open(u'c:/count.txt', 'w')
fout.write('\n'.join(alist)) # 可以通过文本文件的行号同样看到效果
##fout.write('%s' % alist)
fout.close()

allen = len(alist) # 总的单词数
nulen = alist.count('') # 空格的数量
print "words' number is",allen
print "null number is",nulen
print "poor words number is", allen-nulen # 实际的单词数目

fin.close()

相关文章

基于python的ini配置文件操作工具类

本文实例为大家分享了python的ini配置文件操作工具类的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python # -*- coding: utf-8 -...

python运行时间的几种方法

最早见过手写的,类似于下面这种: import datetime def time_1(): begin = datetime.datetime.now() sum =...

python实战之实现excel读取、统计、写入的示例讲解

背景 图像领域内的一个国内会议快要召开了,要发各种邀请邮件,之后要录入、统计邮件回复(参会还是不参会等)。如此重要的任务,老师就托付给我了。ps: 统计回复邮件的时候,能知道谁参会或谁不...

Python弹出输入框并获取输入值的实例

使用自带的Tkinter模块,简单的弹输入框示例,返回输入值 from Tkinter import * import tkMessageBox def getInput(t...

使用python3批量下载rbsp数据的示例代码

使用python3批量下载rbsp数据的示例代码

1. 原始网站 https://www.rbsp-ect.lanl.gov/data_pub/rbspa/ 2. 算法说明 进入需要下载的数据所在的目录,获取并解析该目录下的信息,解析出...