Python config parser to get all the values from a section?

PythonParsingConfigparser

Python Problem Overview


I want to get all the values from a section using config parser

I used this but it gives only the first value

def ConfigSectionMap(section):
  dict1 = {}
  options = Config.options(section)
  for option in options:
    try:
      dict1[option] = Config.get(section, option)
      if dict1[option] == -1:
        DebugPrint("skip: %s" % option)
    except:
      print("exception on %s!" % option)
      dict1[option] = None
    return dict1


  Config = ConfigParser.ConfigParser()
  Config.read("/etc/harvest.conf")
  print ConfigSectionMap("files").values()

Python Solutions


Solution 1 - Python

Make it a dict:

dict(Config.items('Section'))

Solution 2 - Python

You can make it a list if ordering is important

list(Config.items('Section'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRishabhView Question on Stackoverflow
Solution 1 - PythonNiclas NilssonView Answer on Stackoverflow
Solution 2 - Pythonjithu83View Answer on Stackoverflow