from opencc import OpenCC cc = OpenCC('t2s') # 't2s'表示繁体转简体 import unicodedata import json def fullwidth_to_halfwidth(input_str): return ''.join([unicodedata.normalize('NFKC', char) for char in input_str]) def clearT(s): s = cc.convert(fullwidth_to_halfwidth(s)) return s.lstrip('n').strip().strip(r'\n') def startwithany(s, l): return any(s.startswith(x) for x in l) f = open(r'E:\tmp\Renpy\script.rpy', encoding='utf-8') start = False keys = [ '$', 'stop', 'scene', 'with', 'pause', 'play', 'show', 'voice', 'hide', 'nvl ', '#', 'yalign', 'linear', 'anchor', 'if', 'return' ] _n = { "z": "老师", "y": "叶雨潇", "nvl_narrator": "旁白", "l": "梁芷柔", "a": "同学A", "b": "同学B", "c": "其他同学", "who": "?", "az": "医生", "k": "护士", "kfc": "服务员", "d": "店员", "bird": "候鸟", "yl": "叶雨潇&梁芷柔", "loli": "小梁芷柔", "e": "同学们", "f": "女同学", "g": "同学", "ab": "同学A&B", "m": "广播", "h": "工作人员", "i": "保安", "j": "李金凡", "bird2": "海鸟", "phone": "电话" } sc = [] b = open(r'D:\datasets\b-corpus\视觉小说\format\transient\transient.txt','w',encoding='utf-8') for line in f: if line.startswith('label start:'): start = True continue if not start: continue line = line.strip() if not line: continue if startwithany(line, keys): continue if line.startswith('"'): n = '旁白' if startwithany(line, _n.keys()) and ' ' in line: idx = line.index(' ') n = line[:idx] n = _n[n] line = line[idx:].strip() 左引号 = line.index('"') 右引号 = line.rindex('"') + 1 d = line[左引号:右引号] sc.append(n + ':' + clearT(d)) b.write('\n'.join(sc)) f.close() b.close() tmp = json.dumps(_n, ensure_ascii=False, indent=2)