python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符
更新時間:2019年08月31日 11:01:30 作者:weiguang1111
這篇文章主要為大家詳細介紹了python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)遞歸查找某個路徑下所有文件中的中文字符,供大家參考,具體內(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') # 處理前進行相關的處理,包括轉(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)過相關處理后得到中文的文本
for s in zh.split():
out.add(s) # 經(jīng)過相關處理后得到中文的文本
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python連接SQL?server數(shù)據(jù)庫并進行簡單查詢的操作詳解
SQL?Server是微軟推出的重量級的數(shù)據(jù)庫,本文將給大家詳細介紹了一下Python連接SQL?server數(shù)據(jù)庫詳細流程,并通過代碼示例給大家講解的非常清除,具有一定的參考價值,需要的朋友可以參考下2024-02-02
Python實現(xiàn)光速定位并提取兩個文件的不同之處
如果你經(jīng)常與Excel或Word打交道,那么從兩份表格/文檔中找到不一樣的元素是一件讓人很頭疼的工作。本文就將以兩份真實的Excel/Word文件為例,講解如何使用Python光速對比并提取文件中的不同之處2022-08-08
Linux RedHat下安裝Python2.7開發(fā)環(huán)境
這篇文章主要為大家詳細介紹了Linux RedHat下安裝Python2.7、pip、ipython環(huán)境、eclipse和PyDev環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

