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通过poll实现异步IO的方法
Jun 04 Python
深入解答关于Python的11道基本面试题
Apr 01 Python
Python排序算法实例代码
Aug 10 Python
浅谈python爬虫使用Selenium模拟浏览器行为
Feb 23 Python
Django实现表单验证
Sep 08 Python
Python面向对象基础入门之编码细节与注意事项
Dec 11 Python
对python中list的拷贝与numpy的array的拷贝详解
Jan 29 Python
简单了解python协程的相关知识
Aug 31 Python
pytorch中的自定义数据处理详解
Jan 06 Python
python GUI库图形界面开发之PyQt5拖放控件实例详解
Feb 25 Python
使用OpenCV实现道路车辆计数的使用方法
Jul 15 Python
Python机器学习应用之工业蒸汽数据分析篇详解
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中一个完整表单处理实现代码
2011/11/10 PHP
PHP实现获取图片颜色值的方法
2014/07/11 PHP
php对象和数组相互转换的方法
2015/05/12 PHP
CodeIgniter读写分离实现方法详解
2016/01/20 PHP
PHP list() 将数组中的值赋给变量的简单实例
2016/06/13 PHP
jQuery对象和DOM对象的相互转化实现代码
2010/03/02 Javascript
使用原生js实现页面蒙灰(mask)效果示例代码
2014/06/20 Javascript
jQuery模拟完美实现经典FLASH导航动画效果【附demo源码下载】
2016/11/09 Javascript
Angular.js实现注册系统的实例详解
2016/12/18 Javascript
javaScript强制保留两位小数的输入数校验和小数保留问题
2018/05/09 Javascript
ES6 Promise对象的含义和基本用法分析
2019/06/14 Javascript
vue2 拖动排序 vuedraggable组件的实现
2019/08/08 Javascript
JS扁平化输出数组的2种方法解析
2019/09/17 Javascript
jQuery实现图片随机切换、抽奖功能(实例代码)
2019/10/23 jQuery
小程序外卖订单界面的示例代码
2019/12/30 Javascript
vue中的过滤器及其时间格式化问题
2020/04/09 Javascript
vue实现简单加法计算器
2020/10/22 Javascript
[03:36]2014DOTA2 TI小组赛综述 八强诞生进军钥匙球馆
2014/07/15 DOTA
python遍历文件夹并删除特定格式文件的示例
2014/03/05 Python
Python模拟随机游走图形效果示例
2018/02/06 Python
python文件操作之批量修改文件后缀名的方法
2018/08/10 Python
python3 批量获取对应端口服务的实例
2019/07/25 Python
python 批量修改 labelImg 生成的xml文件的方法
2019/09/09 Python
wxPython实现整点报时
2019/11/18 Python
解决python replace函数替换无效问题
2020/01/18 Python
解决windows上安装tensorflow时报错,“DLL load failed: 找不到指定的模块”的问题
2020/05/20 Python
python 窃取摄像头照片的实现示例
2021/01/08 Python
《小鹰学飞》教学反思
2014/04/23 职场文书
高三上学期学习自我评价
2014/04/23 职场文书
工资收入证明
2014/10/07 职场文书
优秀少先队员事迹材料
2014/12/24 职场文书
考研复习计划
2015/01/19 职场文书
感谢信的格式
2015/01/21 职场文书
电气工程师岗位职责
2015/02/12 职场文书
2016年中学法制宣传日活动总结
2016/04/01 职场文书
MySQL实现配置主从复制项目实践
2022/03/31 MySQL