Skip to content

Instantly share code, notes, and snippets.

@Saharayama
Created January 29, 2025 13:04
Show Gist options
  • Select an option

  • Save Saharayama/d64d3e66ece524734e83b2389bea6a18 to your computer and use it in GitHub Desktop.

Select an option

Save Saharayama/d64d3e66ece524734e83b2389bea6a18 to your computer and use it in GitHub Desktop.
import glob
from natsort import natsorted
import sys
sys.stdout.reconfigure(encoding="utf-8")
def extract_content_below_keywords(file_paths, keywords):
"""
複数の Shift-JIS テキストファイルを読み込み、複数のキーワードそれぞれについて、
マッチする行の1行下の内容を抽出する。
Args:
file_paths (list): ファイルパスのリスト。glob.glob() で取得したパスなどを渡す。
keywords (list): 検索キーワードのリスト。
Returns:
dict: キーワードをキー、マッチした行の1行下の内容をリストとして格納した辞書。
"""
extracted_contents = {}
for keyword in keywords:
extracted_contents[keyword] = [] # キーワードに対応するリストを初期化
for file_path in file_paths:
try:
with open(file_path, "r", encoding="shift_jis") as f:
lines = f.readlines()
for keyword in keywords:
for i, line in enumerate(lines):
if keyword in line.strip():
if i + 1 < len(lines): # 最終行でないことを確認
next_line = lines[i + 1].strip()
extracted_contents[keyword].append(next_line)
# break # キーワードが見つかったら次のキーワードへ
except FileNotFoundError:
print(f"ファイル {file_path} が見つかりません。")
except UnicodeDecodeError:
print(
f"ファイル {file_path} のデコードに失敗しました。Shift-JIS 形式であることを確認してください。"
)
return extracted_contents
if __name__ == "__main__":
# 1. ファイルパスのリストを取得
file_paths = glob.glob("*.txt")
file_paths = natsorted(file_paths)
print(file_paths)
# 2. 検索キーワードのリストを指定
keywords = [""]
# 3. 抽出処理を実行
extracted_contents = extract_content_below_keywords(file_paths, keywords)
# 4. 結果を表示
for keyword, contents in extracted_contents.items():
print(f"--- 検索キーワード: {keyword} ---")
if contents:
for content in contents:
print(content)
else:
print("一致する行は見つかりませんでした。")
print() # 空行で区切る
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment