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学习数据结构实例代码
May 11 Python
Python基础中所出现的异常报错总结
Nov 19 Python
Python实现两个list求交集,并集,差集的方法示例
Aug 02 Python
解决Python下imread,imwrite不支持中文的问题
Dec 05 Python
python时间序列按频率生成日期的方法
May 14 Python
Django CBV类的用法详解
Jul 26 Python
python关闭占用端口方式
Dec 17 Python
如何基于Python创建目录文件夹
Dec 31 Python
Python面向对象封装操作案例详解 II
Jan 02 Python
在主流系统之上安装Pygame的方法
May 20 Python
Python Request类源码实现方法及原理解析
Aug 17 Python
利用python批量爬取百度任意类别的图片的实现方法
Oct 07 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 验证码制作(网树注释思想)
2009/07/20 PHP
Yii遍历行下每列数据的方法
2016/10/17 PHP
laravel实现前后台路由分离的方法
2019/10/13 PHP
php加速缓存器opcache,apc,xcache,eAccelerator原理与配置方法实例分析
2020/03/02 PHP
JavaScript 替换Html标签实现代码
2009/10/14 Javascript
jQuery 表格工具集
2010/04/25 Javascript
jQuery Ajax方法调用 Asp.Net WebService 的详细实例代码
2011/04/27 Javascript
JavaScript高级程序设计(第3版)学习笔记3 js简单数据类型
2012/10/11 Javascript
重写javascript中window.confirm的行为
2012/10/21 Javascript
node.js中的events.emitter.listeners方法使用说明
2014/12/10 Javascript
JS+CSS实现仿触屏手机拨号盘界面及功能模拟完整实例
2015/05/16 Javascript
js中不同的height, top的区别对比
2015/09/24 Javascript
JavaScript实现的伸展收缩型菜单代码
2015/10/14 Javascript
AngularJS入门教程之静态模板详解
2016/08/18 Javascript
Bootstrap如何创建表单
2016/10/21 Javascript
js多个物体运动功能实例分析
2016/12/20 Javascript
AngularJS封装$http.post()实例详解
2017/05/06 Javascript
VueJs单页应用实现微信网页授权及微信分享功能示例
2017/07/26 Javascript
Vue-cli中为单独页面设置背景色的实现方法
2018/02/11 Javascript
vue实现行列转换的一种方法
2019/08/06 Javascript
JS 遍历 json 和 JQuery 遍历json操作完整示例
2019/11/11 jQuery
python使用正则表达式匹配字符串开头并打印示例
2017/01/11 Python
Python实现向服务器请求压缩数据及解压缩数据的方法示例
2017/06/09 Python
django定期执行任务(实例讲解)
2017/11/03 Python
python操作redis方法总结
2018/06/06 Python
Python实现的简单读写csv文件操作示例
2018/07/12 Python
selenium+python实现自动化登录的方法
2018/09/04 Python
Python中使用遍历在列表中添加字典遇到的坑
2019/02/27 Python
python3实现钉钉消息推送的方法示例
2019/03/14 Python
HTML5 本地存储之如果没有数据库究竟会怎样
2013/04/25 HTML / CSS
新加坡最早生产电动滑板车的制造商之一:FunsToTheFore
2020/09/08 全球购物
WebSphere 应用服务器都支持哪些认证
2013/12/26 面试题
安全保证书怎么写
2015/02/28 职场文书
我在伊朗长大观后感
2015/06/16 职场文书
教你利用python实现企业微信发送消息
2021/05/23 Python
浅谈resultMap的用法及关联结果集映射
2021/06/30 Java/Android