*disclaimer
1197209
[Python]
book5.py
book5.py(218)
# copyleft 2022-08-15 sugiura@nagoya-u.jp
#
# 評価5のブックレポートデータの整形
# テキストファイルの切り貼りから、タブ区切りファイルにして一覧できるように
import re
with open('book5.txt', mode='r', encoding='UTF-8') as f:
lines = f.read()
lines = re.sub(r'^\s*\n$', '', str(lines))
lines = re.sub(r'[-━]+', '', lines)
lines = re.sub(r'\s+', ' ', lines)
lines = re.sub('(書名:)', '\n', lines)
lines: str = re.sub(r'(著者名:|出版社:|レベル:)', '\t', lines)
lines = lines.title()
with open('book5out.txt', 'w') as o:
print(str(lines), file=o)
サンプル
入力ファイル
書名:The New Zealand File 著者名:Richard MacAndrew 出版社:CAMBRIDGE UNIVERSITY PRESS レベル:2 - - - - - - - - - - - - - - - - - - - - 書名:Love Story 著者名:Rosemary Border 出版社:Oxford University Press レベル:3 ------------------------------------------------------------ 書名:THE MOSQUITO COAST 著者名:PAUL THEROUX 出版社:Longman レベル:4 書名:The Double Bass Mystery 著者名:Jeremy Harmer 出版社:CAMBRIDGE UNIVERSITY PRESS レベル:2 書名:The Canterville Ghost and Other Stories 著者名:Oscar Wilide 出版社:Macmillan Readers レベル:3
出力ファイル
The New Zealand File Richard Macandrew Cambridge University Press 2 Love Story Rosemary Border Oxford University Press 3 The Mosquito Coast Paul Theroux Longman 4 The Double Bass Mystery Jeremy Harmer Cambridge University Press 2 The Canterville Ghost And Other Stories Oscar Wilide Macmillan Readers 3
https://sugiura-ken.org/wiki/