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() def startsWithAny(s: str, keys): for x in keys: if s.startswith(x): return x else: return False def startsWithAlnum(s: str, _chrs): retn = '' for char in s: if (char.isascii() and char.isalnum()) or (char in _chrs): retn += char else: break return retn def listRfind_idx(_i, _data, _condition, delta=1): for j in range(_i - 1, -1, -1): if _condition(_data[j]): return range(j+delta, _i+1) return range(0, _i+1) def startsWithCmd(s: str, _chrs=None): if _chrs is None: _chrs = {'_', '@'} cmd = startsWithAlnum(s, _chrs) if cmd: return s.startswith(cmd + ' ') else: return False # ================= a = get_all_files_in_directory(r'E:\tmp\素晴日', ext='.txt') b = r'D:\datasets\tmp' # ================= sc = {} _n = { "由岐": "由岐", "卓司": "卓司", "司": "司", "镜": "镜", "柘榴": "柘榴", "镜·司": "镜&司", "柘榴·镜": "柘榴&镜", "司?柘榴": "司&柘榴", "一同": "一同", "美羽": "美羽", "彩名": "彩名", "布偶": "布偶", "陌客": "陌客", "本": "本", "槝劶尷奒": "大叔", "女子校生": "同校女生", "男子校生": "同校男生", "清川": "清川", "瀕名川": "濒名川", "先生": "先生", "男子学生": "男学生", "男子": "男子", "男子生徒": "男学生", "洁": "洁", "安子": "安子", "颔田": "颔田", "閭宫": "闾宫", "羽咲": "羽咲", "希实香": "希实香", "潔": "洁", "校内广播": "校内广播", "大婶": "大婶", "间宫": "间宫", "皆守": "皆守", "城山": "城山", "沼田": "沼田", "饭沼": "饭沼", "西村": "西村", "西山": "西山", "莉露露": "莉露露", "教師": "教师", "机": "机", "惠": "惠", "聪子": "聪子", "濑名川": "濑名川", "饭田": "饭田", "明日美": "明日美", "信者": "信者", "明日美父亲": "明日美父亲", "明日美母亲": "明日美母亲", "大妈": "大妈", "男行人": "男行人", "女行人": "女行人", "公司职员": "公司职员", "观众": "观众", "神": "神", " 希实香": "希实香", "店长": "店长", "店员": "店员", "男店员": "男店员", "男性教师": "男性教师", "女性教师": "女性教师", "中年男性教师": "中年男性教师", "女子生徒": "女学生", "教师": "教师", "男学生": "男学生", "女学生": "女学生", "别的学校的学生": "别的学校的学生", "不良们": "不良们", "宇佐美": "宇佐美", "亚由美": "亚由美", "恶棍": "恶棍", "Master": "Master", "客A": "客A", "客B": "客B", "木村": "木村", "老师": "老师", "男生": "男生", "女生": "女生", "人妖": "人妖", "客": "客", "齐声": "齐声", "浩夫": "浩夫", "学员": "学员", "练习生": "练习生", "水上": "水上", "琴美": "琴美", "卓司?琴美": "卓司&琴美" } # ================= for path in a: name = path[path.rindex('\\'):] if name not in sc: sc[name] = [] print(name) # ================= with open(path, 'r', encoding='utf-8') as f: data = list(filter(lambda x: (not x.startswith(';')) and (x.endswith('\\') or x.endswith('/')), (x.strip() for x in f.readlines()))) w_i = -1 while w_i < len(data) - 1: w_i += 1 line = data[w_i] if not line.endswith('\\'): continue idx = listRfind_idx(w_i, data, lambda x: x.endswith('\\'), delta=1) tmp = [data[j].strip('\\/') for j in idx if data[j]] if tmp[0].startswith('['): assert ']' in tmp[0] idx = tmp[0].index(']') n = tmp[0][1:idx] tmp[0] = tmp[0][idx+1:] if n in _n: n = _n[n] else: _n[n] = clearT(n) print(n, tmp) else: n = '旁白' d = '\\n'.join(tmp) d = clearT(d) if d: sc[name].append(n + ':' + d) # ================= 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)