str_replace替换一次Demo

本文介绍了一个简单的PHP函数,该函数用于在字符串中仅替换首次出现的目标字符串。通过使用此函数,可以有效地更新文本内容中的特定部分,例如将普通文本转换为带有样式的超链接。
//替换一次
function str_replace_once($needle, $replace, $haystack) {

    $pos = strpos($haystack, $needle);
    if ($pos === false) {
        // Nothing found
        return $haystack;
    }
    return substr_replace($haystack, $replace, $pos, strlen($needle));
}
$body = $this->str_replace_once($tagname,'<a href="'.$url.'" title="'.$tagname.'" style=" font-weight:bold">'.$tagname.'</a>',$body);
#!/usr/bin/python # -*- coding: UTF-8 -*- import openpyxl import os # 获取当前脚本所在目录 script_dir = os.path.dirname(__file__) # 构建相对路径(文件直接在当前目录下) excel_file_path = os.path.join(script_dir, '2015.xlsx') txt_file_path = os.path.join(script_dir, 'Demo', 'Demo_2015_SM_Tx_InterfaceTest.txt') output_file_path = os.path.join(script_dir, 'Generate', 'Generate_2015_SM_Tx_InterfaceTest.txt') # 确保输出目录存在 output_dir = os.path.dirname(output_file_path) if output_dir and not os.path.exists(output_dir): os.makedirs(output_dir, exist_ok=True) txt_file_dir = os.path.dirname(txt_file_path) if txt_file_dir and not os.path.exists(txt_file_dir): os.makedirs(txt_file_dir, exist_ok=True) # 检查输出文件是否存在,如果存在则删除 if os.path.exists(output_file_path): os.remove(output_file_path) # 读取Excel文件 wb = openpyxl.load_workbook(excel_file_path) # 读取工作簿的表格,如果不存在则抛出异常 sheet_name = '2015Tx_short_InterfaceTest' if sheet_name in wb.sheetnames: sheet = wb[sheet_name] else: raise ValueError(f"The sheet '{sheet_name}' does not exist in the workbook.") # 读取demo文件的内容 with open(txt_file_path, 'r', encoding='utf-8') as file: content = file.read() # 替换并保存结果 with open(output_file_path, 'w', encoding='utf-8') as output_file: for i in range(1, 501): # 从1到500 # 获取Excel的值 a_value = sheet[f'A{i}'].value b_value = sheet[f'B{i}'].value c_value = sheet[f'C{i}'].value d_value = sheet[f'D{i}'].value g_value = sheet[f'G{i}'].value h_value = sheet[f'H{i}'].value i_value = sheet[f'I{i}'].value if a_value is None: continue # 替换文本文件 modified_content = content.replace('SPN6601_VSR_IntChrgReq', str(b_value)) modified_content = modified_content.replace('VSR', str(a_value)) modified_content = modified_content.replace('TG1', 'TG'+str(c_value)) modified_content = modified_content.replace('SC1', 'SC'+str(d_value)) modified_content = modified_content.replace('MIN_Value', str(g_value)) modified_content = modified_content.replace('MID_Value', str(h_value)) modified_content = modified_content.replace('MAX_Value', str(i_value)) # 写入到txt文件中,每个结果后加一个换行符 output_file.write(modified_content + '\n\n') str(i_value)我希望拿到i列的值,而不是公式
最新发布
08-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值