Help with ConfigParser

Discussion in 'Python' started by tony.ha@philips.com, Oct 2, 2006.

  1. Guest

    Hello I use ConfigParser as show below to read a config.txt file;

    from ConfigParser import ConfigParser

    config = ConfigParser()
    config.read('config.txt')
    items = config.items('FV')
    for item in items:
    module_name = item[0]
    print module_name


    The config.txt file has the following

    [FV]
    # Set the module to "1" to enable the regression test run on it, otherwise
    set it to "0"

    ip_dtlmmio0_1803: 0
    ip_gpio0_4004: 0
    ip_dmac0_1903: 0
    ip_ptA_a_sdramc_2022: 1
    ip_timer0_3012: 0



    the output has been convert to lowercase, i.e ip_ptA_a_sdramc_2022 become
    ip_pta_a_sdramc_2022
    (the captial letter 'A, become lower case 'a').

    Question: How can I pervent ConfigParse to convert Upper case yo lower
    case??, thanks.
    , Oct 2, 2006
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > Question: How can I pervent ConfigParse to convert Upper case yo lower
    > case??, thanks.


    http://docs.python.org/dev/lib/RawConfigParser-objects.html

    """
    optionxform(option)

    Transforms the option name option as found in an input file or as passed in
    by client code to the form that should be used in the internal structures.
    The default implementation returns a lower-case version of option;
    subclasses may override this or client code can set an attribute of this
    name on instances to affect this behavior. Setting this to str(), for
    example, would make option names case sensitive.
    """"

    If you don't pass defaults:

    config = ConfigParser()
    config.optionxform = str
    # ...

    Or, to be on the safe side:

    class MyCasePreservingConfigParser(ConfigParser):
    optionxform = str

    config = MyCasePreservingConfigParser()
    # ...

    Peter
    Peter Otten, Oct 2, 2006
    #2
    1. Advertising

  3. TonyHa Guest

    Hello Peter,

    Thanks for your help, and it works now!

    Tony.

    Peter Otten wrote:
    > wrote:
    >
    > > Question: How can I pervent ConfigParse to convert Upper case yo lower
    > > case??, thanks.

    >
    > http://docs.python.org/dev/lib/RawConfigParser-objects.html
    >
    > """
    > optionxform(option)
    >
    > Transforms the option name option as found in an input file or as passed in
    > by client code to the form that should be used in the internal structures.
    > The default implementation returns a lower-case version of option;
    > subclasses may override this or client code can set an attribute of this
    > name on instances to affect this behavior. Setting this to str(), for
    > example, would make option names case sensitive.
    > """"
    >
    > If you don't pass defaults:
    >
    > config = ConfigParser()
    > config.optionxform = str
    > # ...
    >
    > Or, to be on the safe side:
    >
    > class MyCasePreservingConfigParser(ConfigParser):
    > optionxform = str
    >
    > config = MyCasePreservingConfigParser()
    > # ...
    >
    > Peter
    TonyHa, Oct 2, 2006
    #3
  4. Enrico Guest

    Hi,
    from the documentation:

    optionxform(option)

    Transforms the option name option as found in an input file or as passed in
    by client code to the form that should be used in the internal structures.
    The default implementation returns a lower-case version of option;
    subclasses may override this or client code can set an attribute of this
    name on instances to affect this behavior. Setting this to str(), for
    example, would make option names case sensitive.

    Bye,
    Enrico
    Enrico, Oct 2, 2006
    #4
    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. Stephen Boulet

    Help with ConfigParser

    Stephen Boulet, May 2, 2004, in forum: Python
    Replies:
    3
    Views:
    396
    Peter Otten
    May 2, 2004
  2. Dan Gass
    Replies:
    0
    Views:
    455
    Dan Gass
    Sep 13, 2004
  3. mwt
    Replies:
    5
    Views:
    505
    Kent Johnson
    Mar 15, 2006
  4. Alexandre CONRAD
    Replies:
    1
    Views:
    286
    Fuzzyman
    Apr 6, 2006
  5. Replies:
    3
    Views:
    428
Loading...

Share This Page