[Python] python ConfigParser: read configuration from string

Discussão em 'Python' iniciado por Stack, Agosto 8, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to parse a configuration string that I receive from a service and read each single paramenter. The string result is [section 1],var1 = 111,var2 = 222

    My code:

    #!/usr/bin/python

    import ConfigParser
    import io
    [...]

    result = decoded['result']
    result = result.replace(',', '\n');
    print result
    config = ConfigParser.RawConfigParser(allow_no_value=True)
    config.readfp(io.BytesIO(result))
    print config.get("section 1", "var2")
    print config.get("section 1", "var1")


    using:

    res = """
    [section 1]
    var1 = 111
    var2 = 222
    """


    it works so I believe is something wrong with result.replace(',', '\n'); but if I print the result seems good. Any suggestion please?

    Thank you dk

    Continue reading...

Compartilhe esta Página