码迷,mamicode.com
首页 > 编程语言 > 详细

python3-对某目录下的文本文件分词

时间:2018-11-20 20:16:45      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:rom   ica   The   example   orm   xpath   rod   reader   read   

from pathlib import Path import os import re pathName=‘./‘ fnLst=list(filter(lambda x:not x.is_dir(),Path(pathName).glob(‘**/*.txt‘))) print(fnLst) for fn in fnLst: with open(fn) as f: print() print(fn) for line in f: for word in re.findall(r‘\w+‘, line): print(word,end="|")

输出结果为:

[PosixPath(‘2.txt‘), PosixPath(‘1.txt‘)]

2.txt
This|tutorial|introduces|the|reader|informally|to|the|basic|concepts|and|features|of|the|Python|language|and|system|It|helps|to|have|a|Python|interpreter|handy|for|hands|on|experience|but|all|examples|are|self|contained|so|the|tutorial|can|be|read|off|line|as|well|
1.txt
Python|is|an|easy|to|learn|powerful|programming|language|It|has|efficient|high|level|data|structures|and|a|simple|but|effective|approach|to|object|oriented|programming|Python|s|elegant|syntax|and|dynamic|typing|together|with|its|interpreted|nature|make|it|an|ideal|language|for|scripting|and|rapid|application|development|in|many|areas|on|most|platforms|

python3-对某目录下的文本文件分词

标签:rom   ica   The   example   orm   xpath   rod   reader   read   

原文地址:http://blog.51cto.com/13959448/2319550

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!