加码

    # 定义英文字母到中文字符的映射
def eng_to_chi(eng_text):
    # eng_to_chi_map = {
    
    #     'a': '王', 'b': '李', 'c': '张', 'd': '刘', 'e': '陈', 'f': '杨', 'g': '赵', 'h': '黄', 'i': '周',
    #     'j': '吴', 'k': '徐', 'l': '孙', 'm': '胡', 'n': '朱', 'o': '庄', 'p': '林', 'q': '何', 'r': '郭',
    #     's': '马', 't': '罗', 'u': '宋', 'v': '郑', 'w': '谢', 'x': '韩', 'y': '唐', 'z': '冯',
        
    #     'A': '萧', 'B': '曹', 'C': '许', 'D': '邓', 'E': '彭', 'F': '曾', 'G': '董', 'H': '袁', 'I': '蒋',
    #     'J': '沈', 'K': '吕', 'L': '卢', 'M': '魏', 'N': '田', 'O': '江', 'P': '姜', 'Q': '范', 'R': '欧',
    #     'S': '石', 'T': '姚', 'U': '谭', 'V': '廖', 'W': '邹', 'X': '熊', 'Y': '金', 'Z': '戴'
    # }
    # 定义英文字母到中文字符的映射
     eng_to_chi_map  = {
        '王': 'a', '李': 'b', '张': 'c', '刘': 'd', '陈': 'e', '杨': 'f', '赵': 'g', '黄': 'h', '周': 'i',
        '吴': 'j', '徐': 'k', '孙': 'l', '胡': 'm', '朱': 'n', '庄': 'o', '林': 'p', '何': 'q', '郭': 'r',
        '马': 's', '罗': 't', '宋': 'u', '郑': 'v', '谢': 'w', '韩': 'x', '唐': 'y', '冯': 'z',
        
        '萧': 'A', '曹': 'B', '许': 'C', '邓': 'D', '彭': 'E', '曾': 'F', '董': 'G', '袁': 'H', '蒋': 'I',
        '沈': 'J', '吕': 'K', '卢': 'L', '魏': 'M', '田': 'N', '江': 'O', '姜': 'P', '范': 'Q', '欧': 'R',
        '石': 'S', '姚': 'T', '谭': 'U', '廖': 'V', '邹': 'W', '熊': 'X', '金': 'Y', '戴': 'Z'
    }
    
    # 使用映射替换文本中的英文字符
    chi_text = ''.join([eng_to_chi_map.get(char, char) for char in eng_text])
    return chi_text

# 读取文件内容并替换
def replace_in_file(input_file_path, output_file_path):
    with open(input_file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    replaced_content = eng_to_chi(content)
    with open(output_file_path, 'w', encoding='utf-8') as file:
        file.write(replaced_content)

# 示例使用
input_file_path = 'example.txt'  # 输入文件路径
output_file_path = 'translated_example.txt'  # 输出文件路径
replace_in_file(input_file_path, output_file_path)

posted @ 2024-05-30 20:42  Ding-yixia  阅读(40)  评论(0)    收藏  举报