def split_sections(s):
 section = None
 content = []
 for line in yield_lines(s):
  if line.startswith('['):
   if line.endswith(']'):
    if (section or content):
     (yield (section, content))
    section = line[1:(-1)].strip()
    content = []
   else:
    raise ValueError('Invalid section heading', line)
  else:
   content.append(line)
 (yield (section, content))

def split_sections ( s ) : section = None content = [ ] for line in yield_lines ( s ) : if line . startswith ( '[' ) : if line . endswith ( ']' ) : if ( section or content ) : ( yield ( section , content ) ) section = line [ 1 : ( - 1 ) ] . strip ( ) content = [ ] else : raise ValueError ( 'Invalid section heading' , line ) else : content . append ( line ) ( yield ( section , content ) )
split a string or iterable thereof into pairs each section is a stripped version of the section header and each content is a list of stripped lines excluding blank lines and comment-only lines .