How to create a dict based on such a file?

Discussion in 'Python' started by Wang Coeus, Feb 14, 2011.

  1. Wang Coeus

    Wang Coeus Guest

    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!
    --
    Coeus
    In the middle of every difficulty lies opportunity.
    -- Albert Einstein
    Wang Coeus, Feb 14, 2011
    #1
    1. Advertising

  2. Wang Coeus

    aspineux Guest

    On 14 fév, 06:47, 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:


    ConfigParser Library does exacly what you want but with .ini file
    format
    [block1]
    key1=value1
    key2=value2
    ....

    Can you change the format of your file ? If so

    import ConfigParser
    config=ConfigParser.RawConfigParser(config_default)
    try:
    config.readfp(open(filename, 'r'))
    except Exception, e:
    logging.error('error reading configuration file %s: %s', filename,
    e)
    sys.exit(1)

    def func(config, key1):
    result={}
    for section in config.sections():
    if config.has_option(section, key1):
    result[section]=config.get(section, key1)
    return result


    If not, you need to parse youre file, and the some question :
    How or what generate this file, is it always the same format ? Could
    it chnage, for exemple for

    block1 { key1=value1 key2=value2 }

    or at least

    block1 {

    key1=value1
    key2=value2


    }

    Is-it big, too big to keep in memory ?



    > ++++++++++++++++++++++++++++++++++++++
    > 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!
    > --
    > Coeus
    > In the middle of every difficulty lies opportunity.
    >                 -- Albert Einstein
    aspineux, Feb 14, 2011
    #2
    1. Advertising

  3. On Feb 14, 6:10 pm, aspineux <> wrote:
    > On 14 fév, 06:47, 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:

    >
    > ConfigParser Library does exacly what you want but with .ini file
    > format
    > [block1]
    > key1=value1
    > key2=value2
    > ...
    >
    > Can you change the format of your file ? If so
    >
    > import ConfigParser
    > config=ConfigParser.RawConfigParser(config_default)
    > try:
    >     config.readfp(open(filename, 'r'))
    > except Exception, e:
    >     logging.error('error reading configuration file %s: %s', filename,
    > e)
    >     sys.exit(1)
    >
    > def func(config, key1):
    >     result={}
    >     for section in config.sections():
    >         if config.has_option(section, key1):
    >             result[section]=config.get(section, key1)
    >     return result
    >
    > If not, you need to parse youre file, and the some question :
    > How or what generate this file, is it always the same format ? Could
    > it chnage, for exemple for
    >
    > block1 { key1=value1 key2=value2 }
    >
    > or at least
    >
    > block1 {
    >
    > key1=value1
    > key2=value2
    >
    > }
    >
    > Is-it big, too big to keep in memory ?
    >
    >
    >
    > > ++++++++++++++++++++++++++++++++++++++
    > > 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!
    > > --
    > > Coeus
    > > In the middle of every difficulty lies opportunity.
    > >                 -- Albert Einstein


    configobj is even better, very similar usage.
    Martin De Kauwe, Feb 14, 2011
    #3
    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:
    403
    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:
    348
    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,302
    Kent Johnson
    Dec 15, 2006
  4. Almad
    Replies:
    8
    Views:
    390
    Terry Reedy
    Dec 14, 2006
  5. Dan Stromberg
    Replies:
    6
    Views:
    212
    Steven D'Aprano
    Feb 15, 2011
Loading...

Share This Page