python3-對某目錄下的文本文件分詞

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|
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章