# -*- coding: utf-8 -*- from knock20 import extraction import re def basic_info(str_): pattern_1 = r'\{\{基礎情報.*?^(.*?)^\}\}$' pattern_2 = r'^\|(.*?)\s\=\s(.*?)$' dic = {} lines = re.findall(pattern_1, str_, re.MULTILINE + re.DOTALL) # DOTALLは.を改行にもマッチさせる lines = re.findall(pattern_2, lines[0], re.MULTILINE) for line in lines: dic[line[0]] = line[1] return dic if __name__ == '__main__': s = extraction('イギリス') for key, value in basic_info(s).items(): print(f'{key}: {value}')
# -*- coding: utf-8 -*- from knock20 import extraction import re def section(s): pattern = r'^(=+)(.*?)=+$' find = re.findall(pattern, s, flags=re.MULTILINE) # ^を複数行に適用させる return {line[1]: len(line[0]) - 1 for line in find} if __name__ == '__main__': str_ = extraction('イギリス') print(section(str_))