替换两个字符间的内容


f = open("1.xml", 'r+', encoding='utf-8')
all_the_lines = f.readlines()
#注意指针问题
f.seek(0)#指针位置为开头
f.truncate(0)#清空文件

# print(f.tell())
for line in all_the_lines:

a = "/data/voice/"
if a in line:
ss = line.split(a)[1]
line = line.replace(ss, '' + "\n") # 加\n目的是为了和下面的对齐

line = line.replace(a, '')
line = line.replace("<Input>", '')
line = line.replace("</Input>", '')

f.write(line)
f.close()

<?xml version="1.0" encoding="utf-8"?>
<Executions>
<Execution name="VoiceCase_Lite-S1_Static_online" type="voice">
<Turn>
<Input>/data/voice/20190725_035326_2_voice_in_dump.wav</Input>
<Action>Action://scenemode/rain_mode</Action>
<ASR>打开雨天模式</ASR>
<Scene>车窗/天窗/空调关闭/车辆静止</Scene>
</Turn>
<Turn>
<Input>/data/voice/20190725_035330_3_voice_in_dump.wav</Input>
<Action>Action://carcontrol/up_air_temperature</Action>
<ASR>调高空调温度</ASR>
<Scene>车窗/天窗/空调关闭/车辆静止</Scene>
</Turn>
<Turn>
<Input>/data/voice/20190725_035335_4_voice_in_dump.wav</Input>
<Action>Action://scenemode/starry_mode</Action>
<ASR>打开星空模式</ASR>
<Scene>车窗/天窗/空调关闭/车辆静止</Scene>
</Turn>
<Turn>
<Input>/data/voice/20190725_035339_5_voice_in_dump.wav</Input>
<Action>Action://carcontrol/down_air_temperature</Action>
<ASR>温度降低点,温度降低点儿</ASR>
<Scene>车窗/天窗/空调关闭/车辆静止</Scene>
</Turn>
<Turn>
<Input>/data/voice/20190725_035343_6_voice_in_dump.wav</Input>
<Action>Action://carcontrol/set_air_temperature</Action>
<ASR>把温度调高到二十六度,把温度调高到26度</ASR>
<Scene>车窗/天窗/空调关闭/车辆静止</Scene>
</Turn>

</Execution>
</Executions>



猜你喜欢

转载自www.cnblogs.com/wyx1990/p/12006267.html