строка для поиска:
filecontent = policy-map PM_QOS_C_V-50-50-0-0
class CM_QOS_C_VOICE
priority level 1
police cir percent 9
class CM_QOS_C_VIDEO
priority level 2 percent 15
class CM_QOS_C_ROUTING
bandwidth remaining percent 1
police cir percent 6
class CM_QOS_C_NETMGT
bandwidth remaining percent 1
police cir percent 6
set mpls experimental topmost 7
class CM_QOS_C_CALLSIG
bandwidth remaining percent 1
set mpls experimental topmost 7
police cir percent 6
class CM_QOS_C_SRV
bandwidth remaining percent 7
queue-limit 4096 packets
police cir percent 20
class CM_QOS_C_PRIORITY
bandwidth remaining percent 7
queue-limit 64 packets
police cir percent 30
....
qos = {'VOICE': {'remainingbwspeed': 990.0,
'remainingpercent': 22,
'string': '#VOICE#'},
'VIDEO': {'remainingbwspeed': 405.0,
'remainingpercent': 9,
'string': '#VIDEO#'}}....
Я хотел бы перебрать извлеченный текстовый файл и заменить значение «процент» в текстовом файле на «новое» значение. Я сделал до сих пор:
- откройте файл построчно, найдите ключ в словаре и напечатайте следующую строку.
Что я не могу найти:
- откройте файл, выполните итерацию по файлу до первого ключа, перейдите к первому проценту и затем замените оставшееся значение пропускной способности.
Код, который я использую до сих пор: [строка выше — это содержимое файла, фрагмент]
with open("./playbooks/qos/policy_map_PM_QOS_C_V-50-50-0-0.cfg", "r") as file:
for i, line in enumerate(file):
for key, value in qos.items():
pattern = re.compile(key)
for match in re.finditer(pattern, line):
print(line)
line1=file.readline()
line2=file.readline()
print(line1)
print(line2)
Но это, кажется, портит итератор.