Help with ConfigParser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tony.ha@philips.com

    #1

    Help with ConfigParser

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

    from ConfigParser import ConfigParser

    config = ConfigParser()
    config.read('co nfig.txt')
    items = config.items('F V')
    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_180 3: 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.

  • Peter Otten

    #2
    Re: Help with ConfigParser

    tony.ha@philips .com wrote:
    Question: How can I pervent ConfigParse to convert Upper case yo lower
    case??, thanks.


    """
    optionxform(opt ion)

    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.optionxf orm = str
    # ...

    Or, to be on the safe side:

    class MyCasePreservin gConfigParser(C onfigParser):
    optionxform = str

    config = MyCasePreservin gConfigParser()
    # ...

    Peter

    Comment

    • TonyHa

      #3
      Re: Help with ConfigParser

      Hello Peter,

      Thanks for your help, and it works now!

      Tony.

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

      >
      """
      optionxform(opt ion)
      >
      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.optionxf orm = str
      # ...
      >
      Or, to be on the safe side:
      >
      class MyCasePreservin gConfigParser(C onfigParser):
      optionxform = str
      >
      config = MyCasePreservin gConfigParser()
      # ...
      >
      Peter

      Comment

      • Enrico

        #4
        Re: Help with ConfigParser

        Hi,
        from the documentation:

        optionxform(opt ion)

        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


        Comment

        Working...