Search code examples
pythonconfigparserconfiguration-files

Assigning more than one value to the same key in configuration file


I am trying to create a configuration file and need to parse it with python using ConfigParser module

For example i had the below settings in a file config.sr

[basic]
basic=bz2,calendar,Core,ctype,curl,date,dom,ereg,exif,fileinfo,filter,ftp,gd,gettext,gmp,mhash,mysql,mysqli,openssl,pcntl,pcre,PDO,pdo_mysql,pdo_sqlite,Phar,readline

[advance]
advance=Reflection,session,shmop,SimpleXML,sockets,SPL,sqlite3,standard,tokenizer,wddx,xdebug,xml,xmlreader,xmlwriter,xsl,zip,zlib,Xdebug

So as you can see there are more values(29) separated with comma and assigned to a single key basic, and working fine when i used ConfigParser module to parse it and fetched the results, but the assigned values are too long to declare.

when i assigned them in multiple lines its displaying some errors, more over the number of values separated by comma will be increased further to 50, due to which it causes some readability problem to move over the cursor forward continuosly.

Finally what all i want to know is how can we declare the the values related to single key in multiple lines in configuration ?

Example Format

[basic]
basic=bz2,calendar,Core,ctype,curl,date,dom,ereg,exif,fileinfo,filter,ftp,gd,gettext,gmp,

hash,iconv,json,libxml,mbstring,mcrypt,mhash,mysql,mysqli,openssl,pcntl,pcre,PDO,pdo_mysql,

pdo_sqlite,Phar,readline

.............

Solution

  • Indent them:

    [basic]
    basic:
        bz2
        calendar
        Core
        ctype
        curl
        date
        dom
        ereg
        exif
        fileinfo
        filter
        ftp
        gd
        gettext
        gmp
        mhash
        mysql
        mysqli
        openssl
        pcntl
        pcre
        PDO
        pdo_mysql
        pdo_sqlite
        Phar
        readline
    

    ConfigParser will then have a list, which you can do what you like with.

    config = ConfigParser.ConfigParser()
    config.read('config.sr')
    basic_list = config.get('basic', 'basic').split('\n')
    

    Edit:

    Running the code as follows: My config.sr:

    [basic]
    basic:
        bz2
        calendar
        Core
        ctype
        curl
        date
        dom
        ereg
        exif
        fileinfo
        filter
        ftp
        gd
        gettext
        gmp
        mhash
        mysql
        mysqli
        openssl
        pcntl
        pcre
        PDO
        pdo_mysql
        pdo_sqlite
        Phar
        readline
    
    [advanced]
    advanced:
        a
        b
        c
        d
        e
    

    And my python file: q_14934291.py:

    import ConfigParser
    
    config = ConfigParser.ConfigParser()
    config.read('config.sr')
    
    basic_list = config.get('basic', 'basic').split('\n')
    print('Basic list:')
    print(basic_list)
    
    advanced_list = config.get('advanced', 'advanced').split('\n')
    print('\n\nAdvanced list:')
    print(advanced_list)
    

    Output of running it:

    Basic list:
    ['', 'bz2', 'calendar', 'Core', 'ctype', 'curl', 'date', 'dom', 'ereg', 'exif', 'fileinfo', 'filter', 'ftp', 'gd', 'gettext', 'gmp', 'mhash', 'mysql', 'mysqli', 'openssl', 'pcntl', 'pcre', 'PDO', 'pdo_mysql', 'pdo_sqlite', 'Phar', 'readline']
    
    
    Advanced list:
    ['', 'a', 'b', 'c', 'd', 'e']
    

    Make sure you're using the right indentation (4 spaces). If you use the wrong indentation, it will cause an error.

    Also, you need to .split('\n') on the entry. I forgot that part sorry.

    If you don't want the empty entry at the start, also strip the input:

    basic_list = config.get('basic', 'basic').strip().split('\n')