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之网站的结构
Oct 24 Python
浅析Python中的序列化存储的方法
Apr 28 Python
浅谈django中的认证与登录
Oct 31 Python
python3中str(字符串)的使用教程
Mar 23 Python
python实现微信跳一跳辅助工具步骤详解
Jan 04 Python
Python网络爬虫神器PyQuery的基本使用教程
Feb 03 Python
Python中实现变量赋值传递时的引用和拷贝方法
Apr 29 Python
基于python批量处理dat文件及科学计算方法详解
May 08 Python
Python逐行读取文件中内容的简单方法
Feb 26 Python
从pandas一个单元格的字符串中提取字符串方式
Dec 17 Python
Python json读写方式和字典相互转化
Apr 18 Python
Python+uiautomator2实现自动刷抖音视频功能
Apr 29 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通过array_unshift函数添加多个变量到数组前端的方法
2015/03/18 PHP
thinkPHP连接sqlite3数据库的实现方法(附Thinkphp代码生成器下载)
2016/05/27 PHP
一种JavaScript的设计模式
2006/11/22 Javascript
WordPress 照片lightbox效果的运用几点
2009/06/22 Javascript
jQuery中的bind绑定事件与文本框改变事件的临时解决方法
2010/08/13 Javascript
JS小功能(列表页面隔行变色)简单实现
2013/11/28 Javascript
jquery退出each循环的写法
2014/02/26 Javascript
js调用浏览器打印模块实现点击按钮触发自定义函数
2014/03/21 Javascript
深入浅析AngularJS中的module(模块)
2016/01/04 Javascript
js 转json格式的字符串为对象或数组(前后台)的方法
2016/11/02 Javascript
jquery实现简单的瀑布流布局
2016/12/11 Javascript
jQuery实现可拖拽3D万花筒旋转特效
2017/01/03 Javascript
JavaScript实现时钟滴答声效果
2017/01/29 Javascript
Vue组件库发布到npm详解
2018/02/17 Javascript
js事件on动态绑定数据,绑定多个事件的方法
2018/09/15 Javascript
详解Vue组件之间通信的七种方式
2019/04/14 Javascript
JS回调函数原理与用法详解【附PHP回调函数】
2019/07/20 Javascript
过滤器vue.filters的使用方法实现
2019/09/18 Javascript
vue-父子组件和ref实例详解
2019/11/10 Javascript
JavaScript enum枚举类型定义及使用方法
2020/05/15 Javascript
js实现直播点击飘心效果
2020/08/19 Javascript
Python实现批量读取word中表格信息的方法
2015/07/30 Python
用python写测试数据文件过程解析
2019/09/25 Python
使用jupyter notebook运行python和R的步骤
2020/08/13 Python
HTML5+CSS3实现拖放(Drag and Drop)示例
2014/07/07 HTML / CSS
英国时尚服饰电商:Boohoo
2017/10/12 全球购物
护士自荐信
2013/10/25 职场文书
商场消防安全责任书
2014/07/29 职场文书
国际商务专业毕业生自我鉴定2014
2014/09/27 职场文书
报到证办理个人委托书
2014/10/06 职场文书
2014年监理个人工作总结
2014/12/11 职场文书
逃课检讨书怎么写
2015/01/01 职场文书
丧事答谢词
2015/01/05 职场文书
毕业欢送会致辞
2015/07/29 职场文书
七年级语文教学反思
2016/03/03 职场文书
python实现高效的遗传算法
2021/04/07 Python