python實(shí)現(xiàn)遞歸查找某個(gè)路徑下所有文件中的中文字符
本文實(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ān)處理后得到中文的文本
for s in zh.split():
out.add(s) # 經(jīng)過相關(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")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實(shí)例
今天小編就為大家分享一篇python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python人工智能之路 jieba gensim 最好別分家之最簡單的相似度實(shí)現(xiàn)
這篇文章主要介紹了Python人工智能之路 jieba gensim 最好別分家之最簡單的相似度實(shí)現(xiàn) ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python pywifi ERROR Open handle fai
這篇文章主要介紹了Python pywifi ERROR Open handle failed問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python工廠模式實(shí)現(xiàn)封裝Webhook群聊機(jī)器人詳解
企業(yè)存在給 特定群組 自動推送消息的需求,你可以在群聊中添加一個(gè)自定義機(jī)器人,通過服務(wù)端調(diào)用 webhook 地址,即可將外部系統(tǒng)的通知消息即時(shí)推送到群聊中。本文就來和大家聊聊具體實(shí)現(xiàn)方法2023-02-02
基于DataFrame篩選數(shù)據(jù)與loc的用法詳解
今天小編就為大家分享一篇基于DataFrame篩選數(shù)據(jù)與loc的用法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python 獲取毫秒數(shù),計(jì)算調(diào)用時(shí)長的方法
今天小編就為大家分享一篇python 獲取毫秒數(shù),計(jì)算調(diào)用時(shí)長的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02

