Search code examples
pythonstringconfigconfigparser

ConfigParser.get returning a string need to convert it to section


I want to use the return value of RawConfigParser.get ('somesection', 'someoption') as the section for another RawConfigParser.get, but in practice the result is a doubly encased string.

section = RawConfigParser.get ('somesection', 'someoption')
subsection = RawConfigParser.get (section, 'someotheroption') # INCORRECT RawConfigParser.get ('"somesection"', 'someotheroption')

How do I avoid this?


Solution

  • You have a couple options, one of which is to use the ast library

    >>> quoted_string = '"this is a quote"'
    >>> quoted_string
    '"this is a quote"'
    >>> import ast
    >>> unquoted_string = ast.literal_eval(quoted_string)
    >>> unquoted_string
    'this is a quote'