introduction to parsing the key_value parser element is the one we are really interested in this matches "word"-- sequence of alphanumeric charactersfollowed by an equals signfollowed by the rest of the line (which may be emptythe restofline is predefined parser element supplied by pyparsing since we want to accumulate results as we go we add parse action ( function referenceto the key_value parser element--this function will be called for every key=value that is matched although pyparsing provides predefined pythonstylecomment parser elementhere we prefer the simpler literal("#"followed by the rest of the line (and thanks to pyparsing' smart operator overloading we were able to write the literal as string because when we concatenated it with another parser element to produce the comment parser elementpyparsing promoted the to be literal(the parser itself is parser element that matches one or more ini_header or key_value parser elementsand that ignores comment parser elements def accumulate(tokens)keyvalue tokens key key lower(if lowercase_keys else key key_values[keyvalue this function is called once for each key=value match the tokens parameter is tuple of the matched parser elements in this case we would have expected the tuple to have the keythe equals signand the valuebut since we used suppress(on the equals sign we get only the key and the valuewhich is exactly what we want the lowercase_keys variable is boolean created in an outer scope and that for pls files is set to true (note that for ease of explanation we have shown this function after the creation of the parseralthough in fact it must be defined before we create the parser since the parser refers to it tryparser parsefile(fileexcept parseexception as errprint("parse error{ }format(err)return {return key_values with the parser set up we are ready to call the parsefile(methodwhich in this example takes the name of pls file and attempts to parse it if the parse fails we output simple error message based on what pyparsing tells us at the end we return the key_values dictionary--or an empty dictionary if the parsing failed--and we ignore the parsefile(method' return value since we did all our processing in the parse action