ZHBPythonTool/translate.py
张宏博 450edbc9bb init
2025-12-19 16:20:53 +08:00

34 lines
861 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
from googletrans import Translator
# 输入输出路径
input_file = "de.csv" # 你上传的德语语言文件
output_file = "zh.csv" # 翻译后的中文语言文件
# 读取 csv逗号分隔
df = pd.read_csv(input_file, header=None)
# 初始化翻译器
translator = Translator()
# 翻译第三列
translated = []
for text in df[2]:
if pd.isna(text): # 跳过空值
translated.append("")
else:
try:
result = translator.translate(str(text), src="de", dest="zh-cn")
translated.append(result.text)
except Exception as e:
print(f"翻译失败: {text} -> {e}")
translated.append(text)
# 替换第三列
df[2] = translated
# 保存
df.to_csv(output_file, index=False, header=False, encoding="utf-8")
print(f"翻译完成,保存为 {output_file}")