Search code examples
pythonstringsearchkeyerror

Keyerror when searching through file with same strings.


Hello I am having a KeyError when searching through a huge file with multiple serial numbers. The issue occurs when ever I am searching through the file and input a serial number that appears twice within the file. I want to search through this text file and print out the serial numbers I need or the user inputs.

This is the error:

    if item[param] != correct:
KeyError: 'APN'

Here is a text example of the file I am searching through:

500,SHOWALL
APN=" "
APU=" "
APP=" "
IPD="127.0.0.1"
DSP=1710
IPU="127.0.0.1"
VWD="2"

600,SHOWALL
APN=""
APU=" "
APP=" "
IPD="127.0.0.1"
DSP=1710
IPU="127.0.0.1"
VWD="2"

700,SHOWALL
APN=" "
APU=" "
APP=" "
IPD="127.0.0.1"
DSP=1710
IPU="127.0.0.1"
VWD="2"

500,SHOWALL
APN=""
APU=" "
APP=" "
IPD="127.0.0.1"
DSP=1710
IPU="127.0.0.1"
VWD="2"

since 500 appears twice in the file it will run into a KeyError.

Here is an example of my code:

def process(infile, outfile, keywords):

    keys = [[k[0], k[1], 0] for k in keywords ]
    endk = None
    with open(infile, "rb") as fdin:
        with open(outfile, "ab") as fdout:
            fdout.write("<" + words + ">" + "\n")
            for line in fdin:
                if endk is not None:
                    fdout.write(line)
                    if line.find(endk) >= 0:
                        fdout.write("\n")
                        endk = None
                else:
                    for k in keys:
                        index = line.find(k[0])
                        if index >= 0:
                            fdout.write(line[index + len(k[0]):].lstrip())
                            endk = k[1]
                            k[2] += 1
    if endk is not None:
        raise Exception(endk + " not found before end of file")
    return keys

This is the definition process I use for the input file, output file, and the keywords to search through the file. This is the output portion of the code:

while (count < (len(setNames))):
    for number, item in enumerate(lst, 0):
        print setNames[count]
        for param, correct in correct_parameters.items():
            if item[param] != correct:
                print('{} = {} which is incorrect'.format(param, item[param]))
                with open(compareResults, "ab") as fdout:
                    fdout.write('{}'.format(setNames[count]) + " " + '{} = {} which is incorrect'.format(param, item[param])+ '\n')
        count += 1

My goal would be to some how allow the program to output the result twice if the serial number appears twice or more. So even if 500 appears two or more times within my text file. I still want it to print correct all the 500 results.

Here is a link to my full code. I did not post the full thing because it is very convoluted and needs some cleaning up to do before anything.

http://pastebin.com/aCe9N8vW

If any more information is need I will post below.


Solution

  • You can try like this,

    if item.get(param) != correct:
    

    KeyError:

    Raised when a mapping (dictionary) key is not found in the set of existing keys.
    

    If you don't want to have an exception, you can use the get() method