python提取文件中的汉字


读取指定目录下的文件,提取文件中的所有汉字

# -*- coding: utf-8 -*-

import os
import io
import re

fo = open("word.txt", "w")

# 遍历指定目录,显示目录下的所有文件名
def each_file(filepath):
  for root, dirs, files in os.walk(filepath):
    for file in files:
      filename = os.path.join(root, file)
      read_file(filename)

def read_file(filename):
  with io.open(filename, 'r', encoding='utf-8', errors='ignore') as fn:
    lines = fn.readlines()
    han = re.compile('"[\u4e00-\u9fff]+"').findall(str(lines))
    for val in han:
      fo.write(val + "\n")



if __name__ == '__main__':
  each_file("src")


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM