代码:

import os
import re


def remove_korean_text(srt_content):
    # 定义韩文字符的Unicode范围
    pattern = re.compile(r'[\uac00-\ud7a3]+', re.UNICODE)
    # 使用正则表达式替换韩文字符为空
    cleaned_content = re.sub(pattern, '', srt_content)
    return cleaned_content


def process_srt_files(directory):
    # 遍历指定目录下的所有文件
    for filename in os.listdir(directory):
        if filename.endswith(".srt"):
            file_path = os.path.join(directory, filename)
            try:
                with open(file_path, 'r', encoding='utf-8') as file:
                    srt_content = file.read()

                # 移除韩文内容
                cleaned_srt_content = remove_korean_text(srt_content)

                # 创建新的文件名
                new_filename = filename.replace(".srt", "_cleaned.srt")
                new_file_path = os.path.join(directory, new_filename)

                # 将清理后的内容写入新的文件
                with open(new_file_path, 'w', encoding='utf-8') as file:
                    file.write(cleaned_srt_content)

                print(f"韩文内容已从 {filename} 中移除,并保存到新文件 {new_filename}")
            except Exception as e:
                print(f"处理文件 {filename} 时发生错误: {e}")


# 替换以下路径为你的目标目录
directory_path = r'C:\xinzi'
process_srt_files(directory_path)

 

posted on 2024-09-25 22:22  大话人生  阅读(7)  评论(0编辑  收藏  举报