RussianDeveloper

Work with configparser (Python 2.7.13 and 3.6.0)

Mar 2nd, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import configparser
  2.  
  3.  
  4. # Helper for Name = ConfigSectionMap("SectionOne")['name']
  5. #            Age = ConfigSectionMap("SectionOne")['age']
  6. def ConfigSectionMap(section):
  7.     dict1 = {}
  8.     options = Config.options(section)
  9.     for option in options:
  10.         try:
  11.             dict1[option] = Config.get(section, option)
  12.         except:
  13.             print("exception on %s!" % option)
  14.             dict1[option] = None
  15.     return dict1
  16.  
  17.  
  18. Config = configparser.ConfigParser()
  19. Config.read_file(open("./tomorrow.ini"))
  20. Name = ConfigSectionMap("SectionOne")['name']
  21. Age = ConfigSectionMap("SectionOne")['age']
  22. print("Hello, %s. You are %s years old." % (Name, Age))
Advertisement
Add Comment
Please, Sign In to add comment