[Python] KeyError although it exists [closed]

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I try to replace a couple of IDs in a description by new IDs. The mapping is done using a dictionary in Python.

    import re

    #Return a list containing every occurrence of "ai":

    redmineToGitlabIdMap = dict()
    redmineToGitlabIdMap[150767] = { 'iid': 1 }
    redmineToGitlabIdMap[150768] = { 'iid': 2 }
    redmineToGitlabIdMap[150769] = { 'iid': 3 }

    description = '*(from redmine: issue id 150767, created on 2024-02-27 by **me**)*\n\n* Relations:\n * copied_to #150767\n * relates #150568\n * relates #150569'
    pattern = r'(\s*\* (?:relates|parent|child|copied_to)) #(\d+)'

    for _, redmineId in re.findall(pattern, description):
    targetGitlabId = redmineToGitlabIdMap[redmineId]


    When I try to run this code, I always get a KeyError for the key 150567, no idea why.

    I also tried to convert the key to an int:

    targetGitlabId = redmineToGitlabIdMap[int(redmineId)]


    Then the error occurs on key 150789 instead of 150767.

    Continue reading...

Compartilhe esta Página