import json 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)] def get_all_files_in_directory(directory, ext=''): 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=lambda x: custom_sort_key(x)) a = get_all_files_in_directory(r'E:\tmp\GINKA', ext='.ks.json') _n = { "流星": "流星", None: '旁白', "銀花": "银花", "アナウンス": "广播", "ひまわり": "阳葵", "ゴン太": "权太", "ギンカ": "Ginka", "先生": "老师", "リン": "凛", "ウメ": "梅", "子どもA": "儿童A", "子どもB": "儿童B", "ポメラニアン": "博美犬", "婆さん": "老婆婆", "草二": "草二", "子どもC": "儿童C", "かき氷屋さん": "刨冰摊老板", "あんず": "杏子", "子どもD": "儿童D", "テレビ": "TV", "ギンカ・ひまわり": "Ginka&阳葵", "ハナ": "花", "クラスメイトA": "同学A", "流星・先生・ひまわり": "流星&阳葵&老师", "担任教師": "班主任", "花憐": "花怜", "ギンカ・銀花": "Ginka&银花", "豆腐屋の次男坊": "豆腐店家的二儿子", "防災放送": "防灾广播", "リンの祖母": "凛的祖母", "ハナの弟": "花的弟弟", "島の男": "岛民", "同級生の女子": "女同学", "大人の男性": "成年男性", "流星・銀花": "流星&银花" } b = r'D:\datasets\tmp' sc = {} for path in a: tmp = path.split('scn') name = tmp[-1][0] 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] # _n[n] = texts[1][2][0] n = _n[n] d = texts[1][2][1] if '\n' in d: print(d) if d: sc[name].append(n + ':' + d) pass 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)) tmp = json.dumps(_n, ensure_ascii=False, indent=2)