Python3处理文件中每个词的方法


Posted in Python onMay 22, 2015

本文实例讲述了Python3处理文件中每个词的方法。分享给大家供大家参考。具体实现方法如下:

''''' 
Created on Dec 21, 2012 
处理文件中的每个词 
@author: liury_lab 
''' 
import codecs 
the_file = codecs.open('d:/text.txt', 'rU', 'UTF-8') 
for line in the_file: 
  for word in line.split(): 
    print(word, end = "|") 
the_file.close() 
# 若词的定义有变,可使用正则表达式 
# 如词被定义为数字字母,连字符或单引号构成的序列 
import re 
the_file = codecs.open('d:/text.txt', 'rU', 'UTF-8') 
print() 
print('************************************************************************') 
re_word = re.compile('[\w\'-]+') 
for line in the_file: 
  for word in re_word.finditer(line): 
    print(word.group(0), end = "|") 
the_file.close() 
# 封装成迭代器 
def words_of_file(file_path, line_to_words = str.split): 
  the_file = codecs.open('d:/text.txt', 'rU', 'UTF-8') 
  for line in the_file: 
    for word in line_to_words(line): 
      yield word 
  the_file.close() 
print() 
print('************************************************************************') 
for word in words_of_file('d:/text.txt'): 
  print(word, end = '|') 
def words_by_re(file_path, repattern = '[\w\'-]+'): 
  the_file = codecs.open('d:/text.txt', 'rU', 'UTF-8') 
  re_word = re.compile('[\w\'-]+') 
 
  def line_to_words(line): 
    for mo in re_word.finditer(line): 
      yield mo.group(0) # 原书为return,发现结果不对,改为yield 
  return words_of_file(file_path, line_to_words) 
print() 
print('************************************************************************') 
for word in words_by_re('d:/text.txt'): 
  print(word, end = '|')

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python按照多个字符对字符串进行分割的方法
Mar 17 Python
浅谈numpy库的常用基本操作方法
Jan 09 Python
PyQt5每天必学之工具提示功能
Apr 19 Python
Python中交换两个元素的实现方法
Jun 29 Python
Python实现简单层次聚类算法以及可视化
Mar 18 Python
详解Python 定时框架 Apscheduler原理及安装过程
Jun 14 Python
Python3.7.0 Shell添加清屏快捷键的实现示例
Mar 23 Python
python3用PyPDF2解析pdf文件,用正则匹配数据方式
May 12 Python
解决django 向mysql中写入中文字符出错的问题
May 18 Python
Python类的继承super相关原理解析
Oct 22 Python
python Timer 类使用介绍
Dec 28 Python
Python语言内置数据类型
Feb 24 Python
Python3读取UTF-8文件及统计文件行数的方法
May 22 #Python
在Python中操作时间之mktime()方法的使用教程
May 22 #Python
Python中的localtime()方法使用详解
May 22 #Python
在Python中操作日期和时间之gmtime()方法的使用
May 22 #Python
Python中的ctime()方法使用教程
May 22 #Python
Python3实现从文件中读取指定行的方法
May 22 #Python
Python3搜索及替换文件中文本的方法
May 22 #Python
You might like
生成sessionid和随机密码的例子
2006/10/09 PHP
PHP常用代码大全(新手入门必备)
2010/06/29 PHP
php方法调用模式与函数调用模式简例
2011/09/20 PHP
PHP+iFrame实现页面无需刷新的异步文件上传
2014/09/16 PHP
PHP的引用详解
2015/02/22 PHP
php动态绑定变量的用法
2015/06/16 PHP
php实现SAE上使用storage上传与下载文件的方法
2015/06/29 PHP
php实现将数据做成json的格式给前端使用
2018/08/21 PHP
一个判断email合法性的函数[非正则]
2008/12/09 Javascript
jQuery制作拼图小游戏
2015/01/12 Javascript
JavaScript编写带旋转+线条干扰的验证码脚本实例
2016/05/30 Javascript
React-intl 实现多语言的示例代码
2017/11/03 Javascript
Vue下的国际化处理方法
2017/12/18 Javascript
基于vue框架手写一个notify插件实现通知功能的方法
2019/03/31 Javascript
js基础之事件捕获与冒泡原理
2019/10/09 Javascript
详解JavaScript中的数据类型,以及检测数据类型的方法
2020/09/17 Javascript
在Python的Django框架下使用django-tagging的教程
2015/05/30 Python
python使用xmlrpclib模块实现对百度google的ping功能
2015/06/02 Python
python实现下载整个ftp目录的方法
2017/01/17 Python
Python tornado队列示例-一个并发web爬虫代码分享
2018/01/09 Python
Python实现上下班抢个顺风单脚本
2018/02/07 Python
详解python的argpare和click模块小结
2019/03/31 Python
简单的命令查看安装的python版本号
2020/08/28 Python
美国蔬菜和植物种子公司:Burpee
2017/02/01 全球购物
英国游戏机和游戏购物网站:365games.co.uk
2018/06/18 全球购物
白俄罗斯大卖场:21vek.by
2019/07/25 全球购物
英国第一独立滑雪板商店:The Snowboard Asylum
2020/01/16 全球购物
汽车维修与检测专业应届生求职信
2013/11/12 职场文书
鸿星尔克广告词
2014/03/21 职场文书
论文答谢词
2015/01/20 职场文书
学期个人自我总结
2015/02/13 职场文书
2016党员发展对象培训心得体会
2016/01/08 职场文书
2016年学校禁毒宣传活动工作总结
2016/04/05 职场文书
升职自荐书
2019/05/09 职场文书
2019年市场部个人述职报告(三篇)
2019/10/23 职场文书
vue.js Router中嵌套路由的实用示例
2021/06/27 Vue.js