Python文本处理

该代码示例展示了如何使用Python处理文本翻译。首先读取Excel翻译对照表,然后遍历enlishtext文件,用等号分割内容。如果找到需要翻译的文本,就用Excel中的翻译替换它,并将新内容写入新的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python文本处理

说明情况:

chinese文件是翻译对照表,enlishtext是系统的翻译文字对照表

1.chinese文件是存放有两个字段,如下图,第一列存放需要翻译的文字,第二例为翻译后的文字

在这里插入图片描述

2.enlishtext文件,内容采用等号进行连接,我们需要将等号后面的文字进行翻译,并写入新的文件

在这里插入图片描述

  1. 先将enlishtext文件进行分割

    for line in content:
        # 使用等号进行分割
        parts = line.split('=')
        if len(parts) > 1:
            # 等号之前的内容info,不处理
            info = parts[0]
            # 等号之后的内容
            deny = parts[1].strip()
    

4.判断chinese中第一例文字是否存在与deny(就是等号分割后的文字,需要翻译的)中

def inString(CRM_info,Excel_info):
    if Excel_info in CRM_info:
        return True
    else:
        return False

5.存在则替换

def crmReplace(CRM_info,substring,replacement):
    newString = CRM_info.replace(substring, replacement)
    return newString

6.生成新的字符串,连接info组成我们需要的字符串

        for i in range(df.shape[0]):
            # 获取表格的第一列内容,存在于CRM中的
            chinese = df.iloc[i, 0]
            # 获取Excel第二列内容,已经翻译的值
            english = df.iloc[i, 1]
            if inString(deny,chinese):
                result = crmReplace(deny,chinese,english)
                    # 将等号之后的内容替换为Excel中的第二列内容
                new_line = info + '= ' + str(result)+ '\n'
                new_lines.append(new_line)
                break
        else:
            new_lines.append(line)

全部代码如下,

import pandas as pd
def inString(CRM_info,Excel_info):
    if Excel_info in CRM_info:
        return True
    else:
        return False
def crmReplace(CRM_info,substring,replacement):
    newString = CRM_info.replace(substring, replacement)
    return newString

# 读取Excel文件
excel_file = 'C:/Users/Administrator/Desktop/test/chinese.xlsx'
df = pd.read_excel(excel_file)

# 读取enlishtext文件内容
file_path = 'C:/Users/Administrator/Desktop/test/enlishtext'
with open(file_path, 'r', encoding='utf-8') as f:
    content = f.readlines()
new_content = content

new_lines = []
for line in content:
    # 使用等号进行分割
    parts = line.split('=')
    if len(parts) > 1:
        # 等号之前的内容info,不处理
        info = parts[0]
        # 等号之后的内容
        deny = parts[1].strip()

        for i in range(df.shape[0]):
            # 获取表格的第一列内容,存在于CRM中的
            chinese = df.iloc[i, 0]
            # 获取Excel第二列内容,已经翻译的值
            english = df.iloc[i, 1]
            if inString(deny,chinese):
                result = crmReplace(deny,chinese,english)
                    # 将等号之后的内容替换为Excel中的第二列内容
                new_line = info + '= ' + str(result)+ '\n'
                new_lines.append(new_line)
                break
        else:
            new_lines.append(line)

    else:
        new_lines.append(line)

# 将修改后的内容写入enlishtext文件
with open("2.txt", 'w', encoding='utf-8') as f:
    f.writelines(new_lines)
f.close()
print("文件替换成功")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值