Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 83659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:48:29+00:00 2026-05-10T21:48:29+00:00

I read the part of the docs and saw that the ConfigParser returns a

  • 0

I read the part of the docs and saw that the ConfigParser returns a list of key/value pairs for the options within a section. I figured that keys did not need to be unique within a section, otherwise the parser would just return a mapping. I designed my config file schema around this assumption, then sadly realized that this is not the case:

>>> from ConfigParser import ConfigParser >>> from StringIO import StringIO >>> fh = StringIO(''' ... [Some Section] ... spam: eggs ... spam: ham ... ''') >>> parser = ConfigParser() >>> parser.readfp(fh) >>> print parser.items('Some Section') [('spam', 'ham')] 

Then I went back and found the part of the docs that I should have read:

Sections are normally stored in a builtin dictionary. An alternative dictionary type can be passed to the ConfigParser constructor. For example, if a dictionary type is passed that sorts its keys, the sections will be sorted on write-back, as will be the keys within each section.

To keep my existing configuration file scheme (which I really like now 😉 I’m thinking of passing a mapping-like object as mentioned above that accumulates values instead of clobbering them. Is there a simpler way to prevent key/value collapse that I’m missing? Instead of making a crazy adapter (that could break if ConfigParser‘s implementation changes) should I just write a variant of the ConfigParser itself?

I feel like this may be one of those ‘duh’ moments where I’m only seeing the difficult solutions.

[Edit:] Here’s a more precise example of how I’d like to use the same key multiple times:

[Ignored Paths] ignore-extension: .swp ignore-filename: tags ignore-directory: bin 

I dislike the comma-delimited-list syntax because it’s hard on the eyes when you scale it to many values; for example, a comma delimited list of fifty extensions would not be particularly readable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:48:30+00:00Added an answer on May 10, 2026 at 9:48 pm

    ConfigParser isn’t designed to handle such conditions. Furthermore, your config file doesn’t make sense to me.

    ConfigParser gives you a dict-like structure for each section, so when you call parser.items(section), I’m expecting similar output to dict.items(), which is just a list of key/value tuples. I would never expect to see something like:

    [('spam', 'eggs'), ('spam', 'ham')] 

    Not to mention, how would you expect the following to behave?:

    parser.get('Some Section', 'spam') 

    Which is the intended way to retrieve values.

    If you want to store multiple values for the same key, I would suggest something like this in your config file:

    [Some Section] spam: eggs, ham 

    And this in your code:

    spam_values = [v.strip() for v in parser.get('Some Section', 'spam').split(',')] 

    Of course, this will only work for values that don’t contain commas themselves or handle quoting. For that, you should employ a more advanced technique (see this and this).

    EDIT: If you don’t mind the extra dependency, You could check out ConfigObj, which natively supports lists as a value type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an Applescript playlist generator. Part of the process is to read the
I read somewhere once that the modulus operator is inefficient on small embedded devices
I read in this article that a company has created a software capable of
I read a R code on loess regression, here is part of it: f.lo<-
I read that the executables for the commands issued using exec() calls are supposed
I am trying to read part of a file and stop and a particular
I read this PHP RegEx page , but either I'm missing something, misreading something,
Having read the threads Is SqlCommand.Dispose enough? and Closing and Disposing a WCF Service
I read the Git manual, FAQ, Git - SVN crash course, etc. and they
Please read the whole question. I'm not looking for an approach to managing multi-lingual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.