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实现跨文件全局变量的方法
Jul 07 Python
Python即时网络爬虫项目启动说明详解
Feb 23 Python
对Python中gensim库word2vec的使用详解
May 08 Python
python验证码识别教程之利用投影法、连通域法分割图片
Jun 04 Python
对python csv模块配置分隔符和引用符详解
Dec 12 Python
Python实现元素等待代码实例
Nov 11 Python
浅谈Python type的使用
Nov 19 Python
Python生成个性签名图片获取GUI过程解析
Dec 16 Python
Python 过滤错误log并导出的实例
Dec 26 Python
python各层级目录下import方法代码实例
Jan 20 Python
在keras里面实现计算f1-score的代码
Jun 15 Python
python 匿名函数与三元运算学习笔记
Oct 23 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 地区分类排序算法
2013/07/01 PHP
php防止sql注入示例分析和几种常见攻击正则表达式
2014/01/12 PHP
PHP XML和数组互相转换详解
2016/10/26 PHP
PHP使用imagick扩展实现合并图像的方法
2017/04/25 PHP
比较全面的event对像在IE与FF中的区别 推荐
2009/09/21 Javascript
jQuery创建平滑的页面滚动(顶部或底部)
2013/02/26 Javascript
下拉菜单点击实现连接跳转功能的js代码
2013/05/19 Javascript
Jquery 改变radio/checkbox选中状态,获取选中的值(示例代码)
2013/12/12 Javascript
jquery组件使用中遇到的问题整理及解决
2014/02/21 Javascript
js实现简单随机抽奖的方法
2015/01/27 Javascript
jQuery实现html元素拖拽
2015/07/21 Javascript
JavaScript:Array类型全面解析
2016/05/19 Javascript
JavaScript实现窗口抖动效果
2016/10/19 Javascript
详解浏览器渲染页面过程
2017/02/09 Javascript
使用ionic在首页新闻中应用到的跑马灯效果的实现方法
2017/02/13 Javascript
ES6入门教程之Iterator与for...of循环详解
2017/05/17 Javascript
关于Vue实现组件信息的缓存问题
2017/08/23 Javascript
javascript中的相等操作符(==与===区别)
2019/12/21 Javascript
Angular+ionic实现折叠展开效果的示例代码
2020/07/29 Javascript
Ant-design-vue Table组件customRow属性的使用说明
2020/10/28 Javascript
关于angular 8.1使用过程中的一些记录
2020/11/25 Javascript
Python微信库:itchat的用法详解
2017/08/14 Python
Python基于最小二乘法实现曲线拟合示例
2018/06/14 Python
Python 删除连续出现的指定字符的实例
2018/06/29 Python
python批量赋值操作实例
2018/10/22 Python
python中类的属性和方法介绍
2018/11/27 Python
Scrapy-Redis结合POST请求获取数据的方法示例
2019/05/07 Python
Python3匿名函数lambda介绍与使用示例
2019/05/18 Python
Python3简单实现串口通信的方法
2019/06/12 Python
Python求两点之间的直线距离(2种实现方法)
2019/07/07 Python
python 实现压缩和解压缩的示例
2020/09/22 Python
常用UNIX 命令(Linux的常用命令)
2015/12/26 面试题
社团文化节邀请函
2014/01/10 职场文书
一年级语文教学反思
2014/02/13 职场文书
党的群众路线对照检查材料(个人)
2014/09/24 职场文书
刑事附带民事代理词
2015/05/25 职场文书