introduction to parsing we can immediately skip blank lines (and use slightly simpler regexes)we also skip comment lines since we expect most lines to be key=value lineswe always try to match the key_value_re regex first if this succeeds we extract the keyand lowercase it if necessary then we add the key and the value to the dictionary if the line is not key=value linewe try to match ini header--and if we get match we simply ignore it and continue to the next lineotherwise we report an error (it would be quite straightforward to create dictionary whose keys are ini headers and whose values are dictionaries of the headerskey-values--but if we want to go that farwe really ought to use the configparser module the regexes and the code are quite straightforward--but they are dependent on each other for exampleif we didn' strip whitespace from each line we would have to change the regexes to allow for leading and trailing whitespace here we found it more convenient to strip the whitespacebut there may be occasions where we do things the other way round--there is no one single correct approach at the end (not shown)we simply return the key_values dictionary one disadvantage of using dictionary in this particular case is that every key-value pair is distinctwhereas in factitems with keys that end in the same number ( "title ""file "and "length "are logically related the playlists py program has function (songs_from_dictionary()not shownbut in the book' source codethat reads in key-value dictionary of the kind returned by the code shown here and returns list of song tuples--something we will do directly in the next subsection playlist data parsing |the playlists py program mentioned in the previous subsection can read and write pls format files in this subsection we will write parser that can read files in format and that returns its results in the form of list of collections namedtuple(objectseach of which holds titlea duration in secondsand filename as usualwe will begin by looking at an extract of the data we want to parsethen we will create suitable bnfand finally we will create parser to parse the data the data extract is shown in figure we have omitted most of the data as indicated by the ellipsis the file must begin with the line #extm each entry occupies two lines the first line of an entry starts with #extinfand provides the duration in seconds and the title the second line of an entry has the filename just like with pls formata negative duration signifies that the duration is unknown pyparsing parser ply parser