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 相关文章推荐
ptyhon实现sitemap生成示例
Mar 30 Python
Python中if __name__ == '__main__'作用解析
Jun 29 Python
python实现在字符串中查找子字符串的方法
Jul 11 Python
Python解析并读取PDF文件内容的方法
May 08 Python
使用sklearn之LabelEncoder将Label标准化的方法
Jul 11 Python
Python退火算法在高次方程的应用
Jul 26 Python
python使用PIL实现多张图片垂直合并
Jan 15 Python
Python multiprocessing多进程原理与应用示例
Feb 28 Python
Python3操作读写CSV文件使用包过程解析
Apr 10 Python
keras实现图像预处理并生成一个generator的案例
Jun 17 Python
如何用python实现一个HTTP连接池
Jan 14 Python
python 利用panda 实现列联表(交叉表)
Feb 06 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
zend framework配置操作数据库实例分析
2012/12/06 PHP
php安装swoole扩展的方法
2015/03/19 PHP
php基本函数汇总
2015/07/09 PHP
最新最全PHP生成制作验证码代码详解(推荐)
2016/06/12 PHP
PHP实现可精确验证身份证号码的工具类示例
2018/05/31 PHP
JavaScript 保存数组到Cookie的代码
2010/04/14 Javascript
javascript hashtable 修正版 下载
2010/12/30 Javascript
基于jQuery替换table中的内容并显示进度条的代码
2011/08/02 Javascript
jquery ajax同步异步的执行最终解决方案
2013/04/26 Javascript
jQuery使用$.ajax进行即时验证的方法
2015/12/08 Javascript
利用jQuery的动画函数animate实现豌豆发射效果
2016/08/28 Javascript
基于JavaScript+HTML5 实现打地鼠小游戏逻辑流程图文详解(附完整代码)
2017/11/02 Javascript
如何在js代码中消灭for循环实例详解
2018/07/29 Javascript
JavaScript 引用类型实例详解【数组、对象、严格模式等】
2020/05/13 Javascript
ES6中的Javascript解构的实现
2020/10/30 Javascript
基于wxpython开发的简单gui计算器实例
2015/05/30 Python
使用Python操作MySQL的一些基本方法
2015/08/16 Python
深入解析Python中的lambda表达式的用法
2015/08/28 Python
一些常用的Python爬虫技巧汇总
2016/09/28 Python
django+tornado实现实时查看远程日志的方法
2019/08/12 Python
解决python明明pip安装成功却找不到包的问题
2019/08/28 Python
python实现贪吃蛇游戏源码
2020/03/21 Python
Python爬虫之爬取淘女郎照片示例详解
2020/07/28 Python
python -v 报错问题的解决方法
2020/09/15 Python
Python自动化测试基础必备知识点总结
2021/02/07 Python
HTML5 Canvas中绘制矩形实例
2015/01/01 HTML / CSS
求职自荐信格式
2013/12/04 职场文书
元宵晚会主持词
2014/03/25 职场文书
经管应届生求职信范文
2014/05/18 职场文书
2015年秋季校长开学典礼致辞
2015/07/29 职场文书
2016大学生社会实践单位评语
2015/12/01 职场文书
机关干部纪律作风整顿心得体会
2016/01/23 职场文书
经典人生语录分享:不畏将来,不念过去,笑对当下
2019/12/12 职场文书
Nginx配置并兼容HTTP实现代码解析
2021/03/31 Servers
解决python绘图使用subplots出现标题重叠的问题
2021/04/30 Python
Python可视化神器pyecharts绘制水球图
2022/07/07 Python