本文實(shí)例為大家分享了python實(shí)現(xiàn)遞歸查找某個(gè)路徑下所有文件中的中文字符,供大家參考,具體內(nèi)容如下
# -*- coding: utf-8 -*- # @ description: # @ author: # @ created: 2018/7/21 import re import sys import os reload(sys) sys.setdefaultencoding("utf8") def translate(str): out = set() line = str.strip().decode('utf-8', 'ignore') # 處理前進(jìn)行相關(guān)的處理,包括轉(zhuǎn)換成Unicode等 p2 = re.compile(ur'[^\u4e00-\u9fa5]') # 中文的編碼范圍是:\u4e00到\u9fa5 zh = " ".join(p2.split(line)).strip() # zh = "\n".join(zh.split()) #dsds經(jīng)過(guò)相關(guān)處理后得到中文的文本 for s in zh.split(): out.add(s) # 經(jīng)過(guò)相關(guān)處理后得到中文的文本 return out def extract_file(path): result = set() try: f = open(path) # 打開文件 lines = f.readlines() for line in lines: string = translate(line) if string: result.update(string) except Exception as e: pass return result def extract(path): result = set() files = os.listdir(path) for file in files: if not file.startswith("."): if not os.path.isdir(path + "/" + file): # 判斷是否是文件夾,不是文件夾才打開ssgsg判斷是否是文件夾,不是文件夾才打開 sub_file = extract_file(path + "/" + file) if sub_file: result.update(sub_file) else: print file child = extract(path + "/" + file) if child: result.update(child) return result if __name__ == '__main__': path = "/Users/common" result = extract(path) res_file = open("result.txt", "w") for s in result: res_file.write(s + "\n")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
