• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python3处理文件中每个词的方法

Python3处理文件中每个词的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python3文件操作,python3 打开文件,python3 写文件,python3读取txt文件,python3 创建文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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 = '|')

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python3处理文件中每个词的方法
  • Python3实现从文件中读取指定行的方法
  • Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法
  • Python3实现将文件归档到zip文件及从zip文件中读取数据的方法

相关文章

  • Python实现从URL地址提取文件名的方法
  • python调用短信猫控件实现发短信功能实例
  • Python图像灰度变换及图像数组操作
  • Python中实现远程调用(RPC、RMI)简单例子
  • python实现360的字符显示界面
  • python登录QQ邮箱发信的实现代码
  • Python执行时间的计算方法小结
  • python使用正则搜索字符串或文件中的浮点数代码实例
  • python33 urllib2使用方法细节讲解
  • python嵌套函数使用外部函数变量的方法(Python2和Python3)

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 在Mac OS上搭建Python的开发环境
    • python 输出一个两行字符的变量
    • python自然语言编码转换模块codecs介绍
    • 用Python登录Gmail并发送Gmail邮件的教程
    • 浅析python递归函数和河内塔问题
    • Python isinstance函数介绍
    • pyqt4教程之实现windows窗口小示例分享
    • python实现ftp客户端示例分享
    • python创建和使用字典实例详解
    • python求素数示例分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有