import json import os import re 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') 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\qk\json', ext='.txt.json') _n = { "": "旁白", "燐": "燐", "萤": "萤", "车内广播": "广播", "何農夫": "何农夫", "なにかA": "怪A", # 村民变成的怪物 "なにかB": "怪B", "何若者": "何若者", "声音": "声音", "サトくん": "佐藤君", # 缠着绷带的白狗 "聪君": "聪君", # 缠着绷带的白狗 "なにかC": "怪C", "ヒヒ": "狒狒", # 追赶燐和蛍的高大狒狒怪物 "群がるなにかA": "群怪A", "群がるなにかB": "群怪B", "群がるなにかC": "群怪C", "小さいオオモト様": "小小的大本大人", # 三间坂家的座敷童 "群がるなにかD": "群怪D", "蛍": "萤", "群がるなにかE": "群怪E", "群がるなにかF": "群怪F", "群がるなにかG": "群怪G", "繰り返すなにかA": "重复的怪A", "繰り返すなにかB": "重复的怪B", "繰り返すなにかC": "重复的怪C", "繰り返すなにかD": "重复的怪D", "繰り返すなにかE": "重复的怪E", "繰り返すなにかF": "重复的怪F", "大人のオオモト様": "成年的大本大人", # ? "しつこいなにかC": "顽固的怪C", "しつこいなにかA": "顽固的怪A", "しつこいなにかB": "顽固的怪B", "系着领带的人影": "系着领带的人影", "何教師": "何教师", "聪": "聪", "何警官": "何警官", "DJゴドー": "DJ戈多", "DJ戈多": "DJ戈多", "何作業": "何作业", "人形": "人形", "人偶": "人偶", "狒狒": "狒狒", "影のようななにかA": "影怪A", # 像影子一样的怪A "影のようななにかB": "影怪B", "影のようななにかC": "影怪C", "燐的母亲": "燐的母亲", "孩子们": "孩子们", "小孩子": "小孩子", "小孩子的母亲": "小孩子的母亲", "少女": "少女", "吉村": "吉村", "大川": "大川", "小小的大本大人": "小小的大本大人", "打着波洛领带的人影": "打着波洛领带的人影" } b = r'D:\datasets\tmp' sc = {} for path in a: tmp = path.split('\\')[-1][:5] name = tmp 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] = n n = _n[n] d = clearT(texts[2]) 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)