如何在Python文件中实现模板变量的查找与替换?
问题
需要将模板文件中的_tNumb_、_sName_、_sNumb_、_ip1_、_ip2_替换为用户输入的值,但现有代码仅重复模板文本,未完成替换。
模板文件内容:
ZOYX:_sName_:IUA:S:BCSU,_sNumb_:AFAST; ZOYP:IUA:_sName_:"_ip1_",,49155:"_ip2_",30,,,49155; ZDWP:_sName_:BCSU,_sNumb_:0,3:_sName_; ZOYS:IUA:_sName_:ACT; ZERC:BTS=58,TRX=_tNumb_::FREQ=567,TSC=0,:DNAME=_sName_:CH0=TCHD,CH1=TCHD,CH2=TCHD,CH3=TCHD,CH4=TCHD,CH5=TCHD,CH6=TCHD,CH7=TCHD:; ZERM:BTS=58,TRX=_tNumb_:LEV=-91; ZERM:BTS=58,TRX=_tNumb_:PREF=N; ZERS:BTS=58,TRX=_tNumb_:U;
现有Python代码:
repeat="y" while repeat == "y": keys=['_ip1_', '_ip2_', '_sName_', '_sNumb_', '_tNumb_'] print(keys) #print(keys[2]) print("+++++++++++++++++++++++++++++1") values=[] #ip1, ip2, sName, sNumb, tNumb = input("Enter the IP address1: "), input("Enter the IP address2: "), input("Enter the station name: "), input("Enter the station number: "), input("Enter the transmitter number: ") ip1, ip2, sName, sNumb, tNumb = 1111, 2222, 3333, 4444, 5555 values.append(ip1) values.append(ip2) values.append(sName) values.append(sNumb) values.append(tNumb) print(values) #print(values[2]) print("+++++++++++++++++++++++++++++2") dictionary={} for i in range(len(keys)): dictionary[keys[i]] = values[i] search_text = dictionary[keys[i]] replace_text = keys[i] print(search_text) print(replace_text) print("+++++++++++++++++++++++++++++3") with open(r'template.txt', 'r') as oFile: rFile = oFile.read() #print(rFile) with open(r'output.txt', 'a') as wFile: wFile.write('\n') wFile.write('\n') wFile.write('\n') wFile.write(rFile) repeat = input("Do you want to continue? (y/n): ") if repeat == "n": break while (repeat!="y" and repeat!="n"): repeat = input("Please enter the correct answer (y/n): ")
期望输出:
ZOYX:3333:IUA:S:BCSU,4444:AFAST; ZOYP:IUA:3333:"1111",,49155:"2222",30,,,49155; ZDWP:3333:BCSU,4444:0,3:3333; ZOYS:IUA:3333:ACT; ZERC:BTS=58,TRX=5555::FREQ=567,TSC=0,:DNAME=3333:CH0=TCHD,CH1=TCHD,CH2=TCHD,CH3=TCHD,CH4=TCHD,CH5=TCHD,CH6=TCHD,CH7=TCHD:; ZERM:BTS=58,TRX=5555:LEV=-91; ZERM:BTS=58,TRX=5555:PREF=N; ZERS:BTS=58,TRX=5555:U;
解决方案
你的代码核心问题是没有对读取的模板文本执行替换操作,只是直接将原模板写入输出文件。需要在读取模板后,遍历占位符与输入值的映射关系,完成文本替换后再写入文件。
修改后的代码:
repeat = "y" while repeat == "y": # 获取用户输入 ip1 = input("Enter the IP address1: ") ip2 = input("Enter the IP address2: ") sName = input("Enter the station name: ") sNumb = input("Enter the station number: ") tNumb = input("Enter the transmitter number: ") # 构建占位符与输入值的映射字典 replace_map = { '_ip1_': ip1, '_ip2_': ip2, '_sName_': sName, '_sNumb_': sNumb, '_tNumb_': tNumb } # 读取模板文件 with open('template.txt', 'r') as oFile: template_content = oFile.read() # 批量替换所有占位符 modified_content = template_content for placeholder, value in replace_map.items(): modified_content = modified_content.replace(placeholder, value) # 将替换后的内容写入输出文件 with open('output.txt', 'a') as wFile: wFile.write('\n\n\n') wFile.write(modified_content) # 询问是否继续执行 repeat = input("Do you want to continue? (y/n): ") while repeat not in ["y", "n"]: repeat = input("Please enter the correct answer (y/n): ")
关键修改说明:
- 实现替换逻辑:读取模板后,通过
str.replace()方法遍历映射字典,逐个替换所有占位符。 - 简化字典构建:直接用键值对形式定义映射关系,比循环构建更简洁易维护。
- 恢复用户输入:将测试用的固定值改回
input(),让程序实际接收用户输入的内容。 - 精简冗余代码:移除不必要的打印语句和中间变量,让逻辑更清晰。
内容的提问来源于stack exchange,提问作者David138
相关产品推荐
相关产品推荐

