2

I would like to store this kind of configuration, with repetition of sections and the keys within each:

[item]
name = 'name1'
url = 'address1'
whatever_var = 'foo1'

[item]
name = 'name2'
url = 'address2'
whatever_var = 'foo2'

...

What config file format would be adequate for such structure?: ConfigParser, ConfigObj, JSON, YAML,...? I have never used any (Python newcomer) and would like to know which one fits best.

Note: I am using Python 2.7 so far.

[EDIT]: Well, I believe it's not just a duplicate question, because I need not just duplicated keys, but: - duplicated sections (with their unique keys inside, which cannot be bropued with other keys from other sections)

Also, I don't ask how to do it in ConfigParser, but which file type fits better in this situation (XML, JSON, custom file,...). In fact, I think that what I want cannot be obtained with ConfigParser nor ConfigObj, and I might try XML, even if it is a bit less human-readable. And, hey, it's never a waste of time learning to deal with XML files.

user2821646
  • 23
  • 1
  • 4
  • If I remember correctly, ConfigParser is `dict` based so I think it would take some work to get it to work with this format. – mgilson Sep 27 '13 at 04:32
  • 2
    It's not really a duplicate question; the referenced question was asking about duplicate values. This question is asking about duplicate keys. – gerardw Sep 02 '16 at 12:17
  • Even if it was a duplicate question, the "original" question doesn't really have good solutions nor does it have an accepted answer. – bfris Oct 16 '20 at 16:09

1 Answers1

0

It looks like you are going to store data in files and you are leaving out the idea of using databases and other types of interactions with remote servers to keep this stuff. Glad you have that straightened out.

Many file types will support this type of data if the data is small. (if it'll fit on your machine). If the data gets to be too big or takes too long to access due to billions of records then you have different problems.

Sometimes the domain of the problem affects which file type to use. If you are making a webserver then you might want to go with JSON or XML for transmission purposes.

If you want to use the format that you have spelled out in your post here is how to read the file.

import configparser

conf = configparser.ConfigParser()

conf.read('FILE.INI')

-here is how to read a value from the file you read (and assuming your [item] will have a unique titles like [Person1])

conf['Person1']['name']

-and then writing the file is pretty close to normal

with open('FILE.INI', 'w') as inifile:

    conf.write(inifile)
Back2Basics
  • 7,406
  • 2
  • 32
  • 45
  • It's a config file for local usage, it will store some info to do an automated install and some other tasks I need automated. There will be around 50 "items" (almost all with same variables, but different values). As some of them may dissapear or change from time to time I don't want to add by hand a number (i.e. item1, item2,...). And I need each item to have their own variables, never mixed with others. I don't know JSON, so I think I will try with XML through lxml. – user2821646 Sep 30 '13 at 17:33