def get_all_files_in_directory(directory, ext=''): import os import re custom_sort_key_re = re.compile('([0-9]+)') def custom_sort_key(s): # 将字符串中的数字部分转换为整数,然后进行排序 return [int(x) if x.isdigit() else x for x in custom_sort_key_re.split(s)] all_files = [] for root, dirs, files in os.walk(directory): for file in files: if file.endswith(ext): file_path = os.path.join(root, file) all_files.append(file_path) return sorted(all_files, key=custom_sort_key) def clearT(): import unicodedata from opencc import OpenCC def full2half(input_str): return ''.join([unicodedata.normalize('NFKC', char) for char in input_str]) cc = OpenCC('t2s') # 't2s'表示繁体转简体 def _clearT(s): s = cc.convert(full2half(s)) return s.strip().replace('\n', '\\n') return _clearT clearT = clearT() # ================= a = get_all_files_in_directory(r'E:\tmp\玉响未来_\scn\scn', ext='.txt.json') b = r'E:\tmp\玉响未来_\text' # ================= # ================= sc = {} _n = { "睦季": "睦季", "白": "白", "由岐奈": "由岐奈", "美陀璃": "美陀璃", "保安": "保安", "花子": "花子", "红河童": "红河童", "另一种红河童": "另一种红河童", "其他红河童": "其他红河童", "蒔奈": "莳奈", "人狼": "人狼", "莳奈": "莳奈", "猿妖": "猿妖", "フェレス": "玩偶", "女学生": "女学生", "六神石的妖异": "六神石的妖异", "其他妖异": "其他妖异", "六神石妖异": "六神石妖异", "古树": "古树", "旁边的古树": "旁边的古树", "更旁边的古树": "更旁边的古树", "大樹": "大树", "权现大人": "权现大人", "マダム・リュスト": "信", "男学生": "男学生", "龙胆": "龙胆", "槌渊的狮子": "槌渊的狮子", "村民": "村民", "老女": "老女", "乳母": "乳母", "乌鸦妖异": "乌鸦妖异", "狐狸妖异": "狐狸妖异", "水妖": "水妖", "其他水妖": "其他水妖", "山中妖异": "山中妖异", "みだり": "声音", "倾谈者": "倾谈者", "オシラサマ": "御白大人", "留斯特夫人": "留斯特夫人", "玛丽安娜": "玛丽安娜", "红": "红", "菲蕾丝": "菲蕾丝", "幼年红河童": "幼年红河童", "二人": "二人", "岩神": "岩神", "罗汉大神": "罗汉大神", "其他罗汉大神": "其他罗汉大神", "枥曲": "枥曲", "枥曲之女": "枥曲之女", "枥曲灵树": "枥曲灵树", "山姥": "山姥", "班主任": "班主任", "班同学": "班同学", "「说是突然跑出一只大猴子,好危险的ー」": "女性的声音", "广纪": "广纪", "千寻": "千寻", "广纪&千寻": "广纪&千寻", "「睦季哥哥让我们赶紧回来」": "广纪的声音", "但是,站在门口的却是那些孩子们。": "千寻的声音", "紅": "红", "老生老气的声音": "老生老气的声音", "女性的声音": "女性的声音", "其他女性的声音": "其他女性的声音", "ヤマウバ": "山姥的声音", "月亮大人": "月亮大人", "蛇哥哥": "蛇哥哥", "蛇弟弟": "蛇弟弟", "妻子锅": "妻子锅", "污秽者": "污秽者", "手紙": "手纸", "「这就叫姜还是老的辣啊」": "白&美陀璃", "警備員": "警备员", "穢れた者": "雄釜", "班同学女子A": "班同学女子A", "担任": "担任", "女学生A": "女学生A", "女学生B": "女学生B", "女学生C": "女学生C", "红河童们": "红河童们", "班同学女子B": "班同学女子B", "班同学女子C": "班同学女子C", "由岐奈&莳奈": "由岐奈&莳奈", "猫川の妖異長": "猫川妖异长", "猫川妖异长": "猫川妖异长", "猫川妖异A": "猫川妖异A", "猫川妖异B": "猫川妖异B", "「怎、怎么会……! 水虎大人、水虎大人……!」": "猫川妖异A&B", "鲤鱼奶奶": "鲤鱼奶奶", "一代": "一代", "ウスノロ": "傻大个", "妖異": "妖异" } # ================= import json for path in a: name = path[path.rindex('\\'):] name = name[14] if name not in sc: sc[name] = [] print(name) # ================= with open(path, 'r', encoding='utf-8') as json_file: data = json.load(json_file) # ================= for texts in data['scenes']: try: for texts in texts['texts']: # print(texts) n = texts[0] if not n: n = '旁白' else: if n in _n: n = _n[n] else: _n[n] = clearT(n).replace('・', '&') print(texts, n) # ================= d = clearT(texts[2]) if d: sc[name].append(n + ':' + d) except KeyError: if type(texts) is not dict: print(texts) # ================= for k, v in sc.items(): if v: with open(b + f'\\{k}.txt', 'w', encoding='utf-8') as f: f.write('\n'.join(v)) # ================= import json tmp = json.dumps(_n, ensure_ascii=False, indent=4)