網站首頁 編程語言 正文
前言
有個需求,遍歷文件夾內的文件,并搜索文件中是否存在特定關鍵字(中文)
代碼
import os
import re
from os import path
def cn_to_unicode(in_str, need_str=True, debug=False):
out = []
for s in in_str:
# 獲得該字符的數值
val = ord(s)
# print(val)
# 小于0xff則為ASCII碼,手動構造\u00xx格式
if val <= 0xff:
hex_str = hex(val).replace('0x', '').zfill(4)
# 這里不能以unicode_escape編碼,不然會自動增加一個'\\'
res = bytes('\\u' + hex_str, encoding='utf-8')
else:
res = s.encode("unicode_escape")
out.append(res)
# 調試
if debug:
print(out)
print(len(out), len(out[0]), len(out[-1]))
# 轉換為str類
if need_str:
out_str = ''
for s in out:
out_str += str(s, encoding='utf-8')
return out_str
else:
return out
def scaner_file (url,key):
file = os.listdir(url)
for f in file:
real_url = path.join (url , f)
if path.isfile(real_url):
file_path = path.abspath(real_url)
with open(file_path,encoding='utf8') as file_obj:
contents = file_obj.read()
res = re.findall(key, contents)
if(res):
print(contents)
exit('Success!')
elif path.isdir(real_url):
scaner_file(real_url)
else:
print("其他情況")
pass
print(real_url)
chinese_key = "你好"
unicode_key = cn_to_unicode(chinese_key)
check_dir = "./result"
scaner_file(check_dir,unicode_key)
原文鏈接:https://blog.csdn.net/qq_35544379/article/details/124531920
相關推薦
- 2022-08-28 Redis唯一ID生成器的實現_Redis
- 2022-07-24 C++超詳細講解樹與二叉樹_C 語言
- 2022-11-08 uni-app在手機上打開背景圖片不顯示
- 2021-11-28 C/C++中的?Qt?StandardItemModel?數據模型應用解析_C 語言
- 2024-02-01 webstorm中Line comment at first column,Block commen
- 2022-05-09 使用Dockerfile實現數據卷的掛載問題(推薦)_docker
- 2022-06-13 ASP.NET?Core依賴注入(DI)講解_實用技巧
- 2022-10-03 docker修改容器的端口、容器名、映射地址以及注意事項_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支