pythonconfigparser

Read specific text using configparser


I have an INI file that I want to read just the station name. The INI file looks like this below

[StationData]
StationName = "DPT11"

[Debug]
ProberTouchDown = "TRUE"

I had just learned that it would be easier to use configparser rather than pandas to parse this text file. I am hoping to just get the "DPT11" or DPT11. How would I do that?

I tried copying some examples on this question. Couldn't really figure it out though. using configparser in python


Solution

  • Using python 3.10.6 try the following:

    Script contents:

    import configparser
    config = configparser.ConfigParser()
    config.read("src.ini")
    
    for key, value in config["StationData"].items():
        print(value)
    

    Output from python3 script.py should be:

    "DPT11"