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()
相关推荐
m0_726365833 分钟前
哈希分分预测系统 打造自适应趋势分析「Python+DeepSeek+PyQt5」
python·qt·哈希算法
vyuvyucd14 分钟前
Qwen-1.8B-Chat昇腾Atlas800TA2部署实战
python
轻竹办公PPT19 分钟前
2026 年工作计划 PPT 内容拆解,对比不同 AI 生成思路
人工智能·python·powerpoint
癫狂的兔子32 分钟前
【Python】【Flask】抽奖功能
开发语言·python·flask
linuxxx1101 小时前
python变量引用的小案例
python
2501_936146041 小时前
烟草叶片病害检测_YOLO11-C3k2-MSBlock模型详解
python
Data_agent1 小时前
Python 编程实战:函数与模块化编程及内置模块探索
开发语言·python
十铭忘1 小时前
windows系统python开源项目环境配置1
人工智能·python
Generalzy2 小时前
langchain deepagent框架
人工智能·python·langchain
栈与堆2 小时前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表