69

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()
2
  • 6
    Your return is not properly indented and your function returns in the first iteration of the for loop. Remove two spaces. Commented Dec 20, 2011 at 16:27
  • Get all the keys from the section without the keys from the DEFAULT section >>> cfg['geometry'].keys() - cfg['DEFAULT'].keys() Commented Oct 6, 2023 at 16:18

2 Answers 2

152

Make it a dict:

dict(Config.items('Section'))
Sign up to request clarification or add additional context in comments.

Comments

16

You can make it a list if ordering is important

list(Config.items('Section'))

1 Comment

Does not work for me (the version with dict does). I get this: *** Error in argument: "(Config.items('Section'))".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.