txt中文翻译成英文

python 复制代码
from deep_translator import GoogleTranslator
import re
import sys

# Set the encoding for stdout to 'utf-8'
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf-8', buffering=1)

# Open input and output files
with open('input.txt', 'r', encoding='gbk') as fin, open('english.txt', 'a+', encoding='utf-8') as fout:
    # Get the number of already translated lines
    translated_lines = len(fout.readlines())
    fout.seek(0, 2)  # Move the file pointer to the end of the file for appending

    # Read and translate text line by line
    for i, line in enumerate(fin, start=1):
        # If this line has already been translated, skip it
        if i <= translated_lines:
            continue

        # Use regular expression to remove special characters
        line = re.sub(r'[^\w\s]', '', line)
        # Translate the text
        translation = GoogleTranslator(source='zh-CN', target='en').translate(line)
        # Print the line number and translation
        print(f"Line {i}: {translation}")
        # Write the translation to the output file and immediately save it
        fout.write(translation + '\n')
        fout.flush()
相关推荐
yaoxin5211232 小时前
384. Java IO API - Java 文件复制工具:Copy 示例完整解析
java·开发语言·python
Greyson12 小时前
Layui表格如何使用第三方插件实现树形展示.txt
jvm·数据库·python
2401_871696522 小时前
mysql行级锁失效的原因排查_检查查询条件与执行计划
jvm·数据库·python
xzal123 小时前
python中,turtle基础知识笔记1
笔记·python·turtle
a9511416423 小时前
CSS如何实现元素隐藏不占位_使用display-none完全移除
jvm·数据库·python
rabbit_pro3 小时前
Python调用onnx模型
开发语言·python
AC赳赳老秦4 小时前
OpenClaw生成博客封面图+标题,适配CSDN视觉搜索,提升点击量
运维·人工智能·python·自动化·php·deepseek·openclaw
m0_493934535 小时前
如何监控AWR数据收集Job_DBA_SCHEDULER_JOBS中的BSLN_MAINTAIN_STATS
jvm·数据库·python
xiaotao1315 小时前
01-编程基础与数学基石:概率与统计
人工智能·python·numpy·pandas