1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| import re import threading from translate import Translator
def write_more(filename, n, extracted_data_1, extracted_data_2): with open(filename, 'w', encoding='utf-8') as file: file.write("{\n") for i in range(len(extracted_data_1)): index = 1 translator = Translator(from_lang="zh", to_lang=language[n]).translate(extracted_data_2[i]) # intl_de.append(translator) word = extracted_data_2[i] + " : " + translator + "\t\t" if index == len(extracted_data_2): file.write(" \"" + extracted_data_1[i] + "\": \"" + translator + "\"\n") else: file.write(" \"" + extracted_data_1[i] + "\": \"" + translator + "\",\n") print(f"{word}{language[n]}已成功写入") file.write("}\n") file.close()
if __name__ == '__main__':
filename = 'C:/Users/HP/Desktop/intl_ene.arb' # 用于被翻译的列表 extracted_data_1 = [] extracted_data_2 = []
# 创建线程列表 threads = []
# 翻译后的列表 name = ["intl_de", "intl_en", "intl_fr", "intl_it", "intl_ja", "intl_ko", "intl_ru", "intl_th_TH", "intl_zh_Hans", "intl_zh_Hant",]
language = ["de", "en", "fr", "it", "ja", "ko", "ru", "th", "zh-cn", "zh-tw",]
# 打开文件并读取每一行 with open(filename, 'r', encoding='utf-8') as file: for line in file: # 使用正则表达式查找第二个引号括起来的文本 # 这里的正则表达式假定引号之间没有其他引号,并且第二个引号之后紧跟内容直到另一个引号 match_1 = re.search(r'"([^"]*)"', line) match_2 = re.search(r'(?<=")([^"]*?)"(?=[^"]*$)', line) # print(match_1) # print(match_2) if match_2 and match_2.group(1): # 如果找到了匹配项,并且匹配项不是空的,则添加到列表中 extracted_data_2.append(match_2.group(1)) extracted_data_1.append(match_1.group(1))
# 打印提取的数据 try: for n in range(len(language)): # 定义文件名,注意这里的'.arb'是后缀 filename = f"C:/Users/HP/Desktop/{name[n]}.arb" print(f'写入文件路径:{filename}') # 使用'w'模式打开文件,如果文件不存在则创建它 # 'w'模式会覆盖文件中的任何现有内容
//加入多线程列表循环启动 t = threading.Thread(target=write_more, args=(filename, n, extracted_data_1, extracted_data_2)) threads.append(t) t.start() for trr in threads: trr.join() except Exception as e: print(e) print('异常结束')
|