我在目錄中有 100 多個檔案,我需要找出哪些檔案包含我要查找的泰語單詞
謝謝
我試試這個,但它不起作用`
import pandas as pd
import re
import os
FOLDER_PATH = r'C:\Users\project'
list = os.listdir(FOLDER_PATH)
def is_name_in_csv(word,csv_file):
with open(csv_file,"r") as f:
data = f.read()
return bool(re.search(word, data))
word = "???????????"
for csv_file in list:
if is_name_in_csv(word,csv_file):
print(f"find the {word} in {csv_file}")
`
uj5u.com熱心網友回復:
你不需要正則運算式。您可以簡單地檢查是否word in fileContents. 另外,我改為list因為paths是list內置的 python 關鍵字。
import os
paths = os.listdir(r'C:\Users\project')
def files_with_word(word:str, paths:list) -> str:
for path in paths:
with open(path, "r") as f:
if word in f.read():
yield path
#if you need to do something after each found file
for filepath in files_with_word("???????????", paths):
print(filepath)
#or if you just need a list
filepaths = [fp for fp in files_with_word("???????????", paths)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535851.html
標籤:Python格式文件泰国
