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.lstrip('n').strip().strip(r'\n').replace('\n', '\\n') return _clearT clearT = clearT() # ================= a = get_all_files_in_directory(r'E:\tmp\人鱼\scn', ext='.ks.json') b = r'D:\datasets\tmp' # ================= sc = {} _n = { "": "旁白", "泉": "泉", "悠": "悠", "清正": "清正", "直": "直", "同時": "同时", "郁美": "郁美", "瑠璃": "瑠璃", "司": "司", "小猫": "小猫", "女学生A": "女学生A", "女学生B": "女学生B", "女学生C": "女学生C", "女学生D": "女学生D", "イズナ": "御堂", "直的母亲": "直的母亲", "日常酱": "日常酱", "大叔": "大叔", "幼女": "幼女", "母亲": "母亲", "男学生E": "男学生E", "阿姨A": "阿姨A", "男学生F": "男学生F", "阿姨B": "阿姨B", "女学生E": "女学生E", "女学生F": "女学生F", "裕": "裕", "事务员": "事务员", "音声": "音声", "中年男性": "中年男性", "中年女性": "中年女性", "年轻女性": "年轻女性", "聪子": "聪子", "聡子": "聡子", "伊岐那": "伊岐那", "女性": "女性", "男性": "男性", "男子生徒A": "男学生A", "女子生徒C": "女学生C", "中年教师": "中年教师", "男子生徒B": "男学生B", "男子生徒D": "男学生D", "男子生徒C": "男学生C", "女子生徒E": "女学生E", "女子生徒F": "女学生F", "女子生徒B": "女学生B", "女性教师": "女性教师", "小摊老板": "小摊老板", "女学生G": "女学生G", "女子生徒A": "女学生A", "女子生徒D": "女学生D", "客服": "客服", "美月": "美月", "女学生A": "女学生A", "女学生D": "女学生D", "女学生G": "女学生G", "幼猫": "小猫", "少女": "少女", "少年": "少年", "店长": "店长", "子猫": "小猫", "加纳": "加纳", "主唱": "主唱", "吉他手": "吉他手", "贝斯手": "贝斯手", "乐队女生1": "乐队女生1", "乐队女生2": "乐队女生2" } # ================= import json for path in a: name = path[path.rindex('\\'):] name = name[:7] 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 n is None: n = '旁白' else: if n in _n: n = _n[n] else: _n[n] = clearT(n) 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(): 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=2)