python3 文本处理_Python3处理文件中每个词的方法

本文实例讲述了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程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值