Re: How to create a dict based on such a file?

Discussion in 'Python' started by Dan Stromberg, Feb 14, 2011.

  1. On Sun, Feb 13, 2011 at 9:47 PM, Wang Coeus <> wrote:
    > Hi all,
    > I am new to python. Currently I encountered a problem, please help me to
    > solve this. Thanks in advance!
    > I have a file like below:
    > ++++++++++++++++++++++++++++++++++++++
    > block1
    > {
    >  key1=value1
    >  key2=value2
    >  key3=value3
    > }
    > block2
    > {
    >  key1=value4
    >  key2=value5
    >  key4=value6
    > }
    > ...
    > blockn
    > {
    >  key1=value7
    >  key2=value8
    >  keyn=valuen
    > }
    > +++++++++++++++++++++++++++++++++++++++
    > Different block may have different keys and even same key in different
    > blocks may have different values.
    >
    > Now I want to get a function, which like this:
    > func(key)
    > and it will return a dictionary as below:
    > func(key1) = [block1:value1,block2:value4,...,blockn:value7]
    > and if one block has no "key1" parameter, it will not include in this
    > dict.
    >
    > Thanks a lot!


    pyparsing should be able to make pretty short work of exactly the
    format you want.

    FWIW, the creator of the many .ini format(s), Microsoft, no longer
    recommends using .ini files.
    Dan Stromberg, Feb 14, 2011
    #1
    1. Advertising

  2. On Mon, 14 Feb 2011 11:25:00 -0800, Dan Stromberg wrote:

    > FWIW, the creator of the many .ini format(s), Microsoft, no longer
    > recommends using .ini files.


    That's because they want people to use the registry.

    INI files are simple, easy to parse, lightweight, and human readable and
    human writable using nothing more than a text editor. For many purposes,
    they are perfectly fine. As I see it, the biggest problems with INI files
    are:

    * the INI file module that comes with Python is quite primitive;

    * there are many slightly different behaviours you might want in an INI
    file, and no clean or obvious way to tell which one you are dealing with
    just from the file.

    E.g. if you repeat a key twice, does the second line override the first,
    or add a second value, or is it an error?

    INI files aren't suitable for everything, but there's no need to avoid
    them just because Microsoft don't want you using them, or because they're
    uncool or something...

    Rant: what I *really hate* is when people use XML just because XML is the
    in-thing, not because they need it. Instead of:

    [main]
    key = value
    food = spam
    colour = green


    you get something like this:


    <?xml version="1.0"?>
    <main>
    <entry name="key" value="value"></entry>
    <entry name="food" value="spam"></entry>
    <entry name="colour" value="green"></entry>
    </main>


    --
    Steven
    Steven D'Aprano, Feb 14, 2011
    #2
    1. Advertising

  3. On Mon, Feb 14, 2011 at 3:06 PM, Steven D'Aprano
    <> wrote:
    > On Mon, 14 Feb 2011 11:25:00 -0800, Dan Stromberg wrote:
    >
    >> FWIW, the creator of the many .ini format(s), Microsoft, no longer
    >> recommends using .ini files.

    >
    > That's because they want people to use the registry.
    >
    > INI files are simple, easy to parse, lightweight, and human readable and
    > human writable using nothing more than a text editor. For many purposes,
    > they are perfectly fine. As I see it, the biggest problems with INI files
    > are:
    >
    > * the INI file module that comes with Python is quite primitive;
    >
    > * there are many slightly different behaviours you might want in an INI
    > file, and no clean or obvious way to tell which one you are dealing with
    > just from the file.


    More to the point, there are many dialects of ini files, and nothing
    more than a defacto standard for how they should work.

    They also stand out like a sore thumb on anything but Windows, and
    look a bit anachronistic on Windows.

    But I don't hate ini files - I just think they aren't that clearly a
    great choice. If you want to use them, fine. If you recommend them,
    fine.
    Dan Stromberg, Feb 15, 2011
    #3
  4. Dan Stromberg

    alex23 Guest

    On Feb 15, 9:06 am, Steven D'Aprano <steve
    > wrote:
    > As I see it, the biggest problems with INI files are:
    >
    > * the INI file module that comes with Python is quite primitive;
    >
    > * there are many slightly different behaviours you might want in an INI
    > file, and no clean or obvious way to tell which one you are dealing with
    > just from the file.


    [...]

    > Rant: what I *really hate* is when people use XML just because XML is the
    > in-thing, not because they need it.


    Could it possibly be then that people use XML because a) the support
    is better and b) the behaviour is more predictable? That sounds more
    like good tool choice than faddism.
    alex23, Feb 15, 2011
    #4
  5. On Mon, 2011-02-14 at 19:41 -0800, alex23 wrote:
    > On Feb 15, 9:06 am, Steven D'Aprano <steve
    > > wrote:
    > > As I see it, the biggest problems with INI files are:
    > >
    > > * the INI file module that comes with Python is quite primitive;
    > >
    > > * there are many slightly different behaviours you might want in an INI
    > > file, and no clean or obvious way to tell which one you are dealing with
    > > just from the file.

    >
    > [...]
    >
    > > Rant: what I *really hate* is when people use XML just because XML is the
    > > in-thing, not because they need it.

    >
    > Could it possibly be then that people use XML because a) the support
    > is better and b) the behaviour is more predictable? That sounds more
    > like good tool choice than faddism.

    But why is the support better? ... faddism.
    Westley Martínez, Feb 15, 2011
    #5
  6. On Mon, Feb 14, 2011 at 8:31 PM, Westley Martínez <> wrote:
    > On Mon, 2011-02-14 at 19:41 -0800, alex23 wrote:
    >> On Feb 15, 9:06 am, Steven D'Aprano <steve
    >> > wrote:
    >> > As I see it, the biggest problems with INI files are:
    >> >
    >> > * the INI file module that comes with Python is quite primitive;
    >> >
    >> > * there are many slightly different behaviours you might want in an INI
    >> > file, and no clean or obvious way to tell which one you are dealing with
    >> > just from the file.

    >>
    >> [...]
    >>
    >> > Rant: what I *really hate* is when people use XML just because XML is the
    >> > in-thing, not because they need it.

    >>
    >> Could it possibly be then that people use XML because a) the support
    >> is better and b) the behaviour is more predictable? That sounds more
    >> like good tool choice than faddism.

    > But why is the support better? ... faddism.


    Well...:
    1) XML was one of the first such interchange formats, so it naturally
    got some press; it was an idea whose time had come. It's widely
    accepted that sometimes a first mover gains an advantage over
    follow-on products, even if it's only temporary.
    2) XML is highly general
    3) XML doesn't suffer from the weird exceptions to general rules that
    something like YAML has

    But in the future, I'll likely lean more on JSON.
    Dan Stromberg, Feb 15, 2011
    #6
  7. On Mon, 14 Feb 2011 19:41:00 -0800, alex23 wrote:

    > On Feb 15, 9:06 am, Steven D'Aprano <steve
    > > wrote:
    >> As I see it, the biggest problems with INI files are:
    >>
    >> * the INI file module that comes with Python is quite primitive;
    >>
    >> * there are many slightly different behaviours you might want in an INI
    >> file, and no clean or obvious way to tell which one you are dealing
    >> with just from the file.

    >
    > [...]
    >
    >> Rant: what I *really hate* is when people use XML just because XML is
    >> the in-thing, not because they need it.

    >
    > Could it possibly be then that people use XML because a) the support is
    > better and b) the behaviour is more predictable? That sounds more like
    > good tool choice than faddism.



    Of course it *could* be, but is it?

    I'm certainly not denying that XML can be useful for many applications,
    nor am I saying that INI files are sufficient for everything. But if all
    you need is:

    key = value

    then why use this?

    <entry name="key" value="value"></entry>

    What benefit does it give? It just increases the number of things that
    can go wrong.


    --
    Steven
    Steven D'Aprano, Feb 15, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    412
    Skip Montanaro
    Aug 15, 2003
  2. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    357
    Alexander Kozlovsky
    May 22, 2006
  3. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,319
    Kent Johnson
    Dec 15, 2006
  4. Almad
    Replies:
    8
    Views:
    397
    Terry Reedy
    Dec 14, 2006
  5. Wang Coeus
    Replies:
    2
    Views:
    210
    Martin De Kauwe
    Feb 14, 2011
Loading...

Share This Page