我試着調試這個劇本,但我不知道啥子是造成錯誤。KeyError異常在Python腳本
list1 = ['<p>Text ([0-9]):(.*)</p>' ,'<p>Text2 ([0-9]):(.*)</p>','<p>Text ([0-9]):(.*)</p>']
list2 = ["<p class='text'>Text \1:\2</p>" ,"<p class='text'>Text \1:\2</p>","<p class='text'>TEXT ([0-9]):(.*)</p>"]
translation = dict(zip(list1, list2))
pattern = re.compile('(%s)' % '|'.join(dicts.list1))
file.close()
file = open(args.file,'r+')
def translate(match):
return dicts.translation[match.group(0)]
with open(args.file, 'r+') as output:
with open(args.file, 'r+') as book:
for line in book:
output.write(pattern.sub(translate, line))
錯誤:
return dicts.translation5[match.group(0)]
KeyError: '<p>Text 1:1-1</p>'
這不是正則表達式。 'dicts.translation'沒有'
Text 1:1-1
'鍵。 – mishik應該包括完整的回溯。 – Marcin
我該如何讓列表充當RE? –