# Parse variable from config file - Python
Data Science for Political and Social Phenomena
Author: Guillaume Redoulès

# Parse variable from config file

You can store variables in an ini file for later executions of the script instead of hardcoding the values in the script. Here is the content of `config.ini` :

    [section1]
    var_a:hello
    var_b:world
    [section2]
    myvariable: 42

Use the configparser library for an easy access and parsing of the file.

    import configparser

    config = configparser.ConfigParser()
    config.read("config.ini")
    var_a = config.get("section1", "var_a")
    var_b = config.get("section1", "var_b")

    myvariable = config.get("section2", "myvariable")

    print(var_a, var_b)

    print(myvariable)

    hello world
    42

### Find an error or bug? Have a suggestion?

Everything on this site is avaliable on GitHub. Head on over and submit an issue. You can also message me directly by email.