Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: Processing config file

  • ConfigParse
[person]
fname = Foo
lname=Bar
phone =    123

# comment

      # deep comment


[company]
name = Acme Corp.
phone = 456

import configparser
import sys

def parse():
  if len(sys.argv) != 2:
    print("Usage: " + sys.argv[0] + "  FILEAME")
    exit()
  filename = sys.argv[1]

  cp = configparser.RawConfigParser()
  cp.read(filename)
  return cp

ini = parse()

for section in ini.sections():
  print(section)
  for v in ini.items(section):
    print("  {}  = {}".format(v[0], v[1]))