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实现从ftp服务器下载文件的方法
Apr 30 Python
python实现批量改文件名称的方法
May 25 Python
Python中利用Scipy包的SIFT方法进行图片识别的实例教程
Jun 03 Python
python3如何将docx转换成pdf文件
Mar 23 Python
详解Python 数据库的Connection、Cursor两大对象
Jun 25 Python
python绘制漏斗图步骤详解
Mar 04 Python
Python 基于FIR实现Hilbert滤波器求信号包络详解
Feb 26 Python
利用python实现凯撒密码加解密功能
Mar 31 Python
基于pytorch中的Sequential用法说明
Jun 24 Python
Python建造者模式案例运行原理解析
Jun 29 Python
Python 爬虫批量爬取网页图片保存到本地的实现代码
Dec 24 Python
python中用Scrapy实现定时爬虫的实例讲解
Jan 18 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
分享PHP header函数使用教程
2013/09/05 PHP
详解PHP序列化反序列化的方法
2015/10/27 PHP
PHP命名空间与自动加载机制的基础介绍
2019/08/25 PHP
tp5框架前台无限极导航菜单类实现方法分析
2020/03/29 PHP
如何使用jquery控制CSS样式,并且取消Css样式(如背景色,有实例)
2013/07/09 Javascript
JavaScript调用ajax获取文本文件内容实现代码
2014/03/28 Javascript
js限制checkbox选中个数以限制六个为例
2014/07/15 Javascript
jQuery中html()方法用法实例
2014/12/25 Javascript
JS实现自适应高度表单文本框的方法
2015/02/25 Javascript
JS实现超炫网页烟花动画效果的方法
2015/03/02 Javascript
javascript 对象数组根据对象object key的值排序
2015/03/09 Javascript
jquery验证邮箱格式并显示提交按钮
2015/11/07 Javascript
简述Matlab中size()函数的用法
2016/03/20 Javascript
javascript设计模式之中介者模式学习笔记
2017/02/15 Javascript
Vue项目查看当前使用的elementUI版本的方法
2018/09/27 Javascript
node中IO以及定时器优先级详解
2019/05/10 Javascript
vue组件 keep-alive 和 transition 使用详解
2019/10/11 Javascript
python实现自动重启本程序的方法
2015/07/09 Python
python实现感知器
2017/12/19 Python
PyQt5每天必学之带有标签的复选框
2018/04/19 Python
使用实现XlsxWriter创建Excel文件并编辑
2018/05/04 Python
python matlibplot绘制3D图形
2018/07/02 Python
Python I/O与进程的详细讲解
2019/03/08 Python
python yield关键词案例测试
2019/10/15 Python
英国领先的鞋类零售商和顶级品牌的官方零售商:Wynsors
2020/02/17 全球购物
Static Nested Class 和 Inner Class的不同
2013/11/28 面试题
大专生简历的自我评价
2013/11/26 职场文书
大二法英学生职业生涯规划范文
2014/02/27 职场文书
《果园机器人》教学反思
2014/04/13 职场文书
《宿建德江》教学反思
2014/04/23 职场文书
竞聘演讲稿怎么写
2014/08/28 职场文书
人大代表选举标语
2014/10/07 职场文书
一个独生女的故事观后感
2015/06/04 职场文书
公司处罚决定书
2015/06/24 职场文书
2016年春季运动会加油稿
2015/07/22 职场文书
CSS控制继承中的height能变为可继承吗
2022/06/10 HTML / CSS