import re # 小説ファイルを読み込み infile = "gakumonno_susume.txt" with open(infile, "r", encoding="shift_jis") as fileobj: text = fileobj.read() # 前処理 ## 前置きの部分を削除 text = re.split("-{5,}", text)[2] ## フッターを削除(自分で考えよう) 回答 ## ルビを削除 text = re.sub("《(.+?)》", "", text) ## 注釈を削除(自分で考えよう) 回答 ## 無駄な改行を削除(自分で考えよう) 回答 ## 全角空白を削除 text = text.replace(" ", "") # 結果をファイルに出力 outfile = "gingatetsudono_yoru_filtered.txt" with open(outfile, "w", encoding="utf-8") as fileobj: fileobj.write(text) print("出力:" + outfile)
# -*- coding: utf-8 -*- import re def main(): #infile = "gakumonno_susume.txt" infile = "gingatetsudono_yoru.txt" with open(infile, "r", encoding="shift_jis") as fileobj: text = fileobj.read() #print(text) # 前処理 ## 前置きの部分を削除 text = re.split("-{5,}", text)[2] #print(text) ## フッターを削除(自分で考えよう) text = re.split("底本:", text)[0] #print(text) ## ルビを削除 text = re.sub("《(.+?)》", "", text) #print