Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Dictionary Throwing KeyError for Some Reason
    primarykey
    data
    text
    <p>In some code I pass a dictionary (TankDict) a string from a list. This throws a KeyError, no matter what letter I put in. When I copied and pasted the dictionary out of the context of the program and passed in the same letters from a list, they came out correctly. I have also run <code>type(TankDict)</code> and it comes back as 'dict'. Here is the dictionary: <code>TankDict = {'E':0, 'F':1, 'G':2, 'H':3, 'I':4, 'J':5, 'K':6, 'L':7, 'M':8, 'N':9, 'O':10, 'P':11, 'Q':12, 'R':13, 'S':14, 'T':15, 'U':16, 'V':17, 'W':18, 'X':19}</code></p> <p>The error: <code>enter code herechannelData[1] = tank_address_dict[channelData[1]] KeyError: 'L'</code> (tank_address_dict is a function argument into which TankDict is passed)</p> <p>the contents of channelData: <code>['447', 'L', '15', 'C']</code></p> <p>Can anyone tell me the (probably simple) reason that this happens?</p> <p>EDIT: Code!</p> <p>This is the function where the error is:</p> <pre><code>def getTankID(channel,tank_address_dict,PTM_dict,channel_ref): rawChannelData = 'NA' for line in channel_ref: if str(channel) in line: rawChannelData = line if(rawChannelData == 'NA'): return -1; channelData = rawChannelData.split(' ') channelData.extend(['','']) channelData[1] = channelData[1][:-1] channelData[3] = channelData[1][-1] channelData[1] = channelData[1][:-1] channelData[2] = channelData[1][1:] channelData[1] = channelData[1][:1] print channelData #debug print 'L' in tank_address_dict print 'E' in tank_address_dict print 'O' in tank_address_dict print 'U' in tank_address_dict print type(tank_address_dict) channelData[1] = tank_address_dict[channelData[1]] channelData[3] = PTM_dict[channelData[3]] return(channelData[1:]) </code></pre> <p>This is the function that calls it:</p> <pre><code>def runFile(model, datafile, time_scale, max_PEs, tank_address_dict, PMT_dict, channel_ref): #add initSerial for ser0-4 while(True): raw_data = datafile.readline() #intake data if(raw_data == ''): break #End while loop if the file is done data = raw_data.split(' ') #break up the parts of each line del data[::2] #delete the human formatting data[2] = data[2][:-1] #rm newline (NOTE: file must contain blank line at end!) TankID = getTankID(data[0], tank_address_dict, PMT_dict,channel_ref) if(TankID == -1): print '!---Invalid channel number passed by datafile---!'; break #check for valid TankID model[TankID[0]][TankID[1]][TankID[2]] = scale(data[2],(0,max_PEs),(0,4096)) createPackets(model) #updateModel(ser0,ser1,ser2,ser3,ser4,packet) data[2] = data[2]*time_scale #scale time time.sleep(data[2]) #wait until the next event print data #debug if(TankID != -1): print '---File',datafile,'finished---' #report errors in file run else: print '!---File',datafile,'finished with error---!' </code></pre> <p>And this is the code that calls that:</p> <pre><code>import hawc_func import debug_options #begin defs model = hawc_func.createDataStruct() #create the data structure TankDict = hawc_func.createTankDict() #tank grid coordinate conversion table PTMDict = hawc_func.createPMTDict() #PMT conversion table log1 = open('Logs/log1.txt','w') #open a logfile data = open('Data/event.txt','r') #open data channel_ref = open('aux_files/channel_map.dat','r') time_scale = 1 #0-1 number to scale nano seconds? to seconds #end defs hawc_func.runFile(model,data,4000,TankDict,PTMDict,time_scale,channel_ref) #hawc_func.runFile(model,data,TankDict,PTMDict) #close files log1.close() data.close() #end close files print '-----Done-----' #confirm tasks finished </code></pre> <p>tank_address_dict is created through this function, run by the 3rd block of code, then passed on through the other two:</p> <pre><code>def createTankDict(): TankDict = {'E':0, 'F':1, 'G':2, 'H':3, 'I':4, 'J':5, 'K':6, 'L': 7, 'M':8, 'N':9, 'O':10, 'P':11, 'Q':12, 'R':13, 'S':14, 'T':15, 'U':16, 'V': 17, 'W':18, 'X':19} return TankDict </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload