zaranaid
zaranaid

Reputation: 65

keyError when trying to get value from dictionary, Python

I can iterate and write to file with no problems in the second loop. But in the third loop when I try to call a value I get

R.write(str(dictList[s])+'\n')
KeyError: '<http://www.Department17.University9.edu/GraduateStudent72>'

Here is the code:

f = open('advisor', 'rw')
e = open('dictionary.txt', 'rw')
R = open ('encodedAdvisor.txt', 'w')

import re

dictList={'key':'val'}
for line1 in e:
    kv=line1.split(',')
    key=str(kv[1])  
    val=str(kv[0])
    dictList.update({key:val})
for k, v in dictList.iteritems():
    R.write(str(k)+':'+v+':'+'\n') 
for line2 in f:
    spo = re.findall(r'(<.*?>)', line2)
    s = str(spo[0].strip())
    o = str(spo[1].strip())
    R.write(str(dictList[s])+'\n')

advisor file :

advisor(<http://www.Department17.University9.edu/GraduateStudent71>,<http://www.Department17.University9.edu/AssociateProfessor7>)
advisor(<http://www.Department5.University9.edu/GraduateStudent97>,<http://www.Department5.University9.edu/AssociateProfessor0>)
advisor(<http://www.Department3.University9.edu/GraduateStudent60>,<http://www.Department3.University9.edu/FullProfessor6>)
advisor(<http://www.Department17.University9.edu/GraduateStudent70>,<http://www.Department17.University9.edu/AssistantProfessor2>)
advisor(<http://www.Department9.University9.edu/UndergraduateStudent260>,<http://www.Department9.University9.edu/FullProfessor9>)
advisor(<http://www.Department3.University9.edu/GraduateStudent59>,<http://www.Department3.University9.edu/AssociateProfessor5>)

dictionary file :

GraduateStudent21158,<http://www.Department17.University9.edu/GraduateStudent66>
GraduateStudent21159,<http://www.Department17.University9.edu/GraduateStudent67>
GraduateStudent21160,<http://www.Department17.University9.edu/GraduateStudent68>
GraduateStudent21161,<http://www.Department17.University9.edu/GraduateStudent69>
GraduateStudent21162,<http://www.Department17.University9.edu/GraduateStudent7>
GraduateStudent21163,<http://www.Department17.University9.edu/GraduateStudent70>
GraduateStudent21164,<http://www.Department17.University9.edu/GraduateStudent71>
GraduateStudent21165,<http://www.Department17.University9.edu/GraduateStudent72>
GraduateStudent21166,<http://www.Department17.University9.edu/GraduateStudent73>
GraduateStudent21167,<http://www.Department17.University9.edu/GraduateStudent74>    

Upvotes: 0

Views: 853

Answers (1)

pylang
pylang

Reputation: 44475

I think you have trailing spaces for each line in your dictionary file. I notice them when I select the text. If this is in your actual file, this will raise an error since the strings will not match during the key lookup. Try this:

for line1 in e:
    line1 = line1.strip()
    ...

Otherwise, test that your expected keys are actually in dictList keys:

expected = "<http://www.Department17.University9.edu/GraduateStudent72>"
assert expected in dictList

If an exception is raised you can track down the differences with difflib. Any printed + or - will indicate the differences. This should help you troubleshoot what's going on.

import difflib

actual = [k for k in dictList if "72" in k][0]
for i in difflib.ndiff(expected, actual):
    print(i, end="")

Upvotes: 1

Related Questions