title: Elegant parsing of text-based key-value list
body: <p>I'm writing a parser for text-based sequence alignment/map (SAM) files. One of the fields is a concatenated list of key-value pairs comprising a single alphabet character and an integer (the integer comes first). I have working code, but it just feels a bit clunky. What's an elegant pattern for parsing a format such as this? Thanks.</p>
  
  <p>Input:</p>
  
  <pre><code>record['cigar_str'] = '6M1I69M1D34M'
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>record['cigar'] = [
      {'type':'M', 'length':6},
      {'type':'I', 'length':1},
      {'type':'M', 'length':69},
      {'type':'D', 'length':1},
      {'type':'M', 'length':34}
  ]
  </code></pre>
  
  <p>EDIT: My current approach</p>
  
  <pre><code>cigarettes = re.findall('[\d]{0,}[A-Z]{1}', record['cigar_str'])
  for cigarette in cigarettes:
      if cigarette[-1] == 'I':
          errors['ins'] += int(cigarette[:-1])
      ...
  </code></pre>