• Latest
  • Trending
ConfigParser – manage user-editable settings for your Python programs

ConfigParser – manage user-editable settings for your Python programs

May 18, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Thursday, 16 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

ConfigParser – manage user-editable settings for your Python programs

by ITECHNEWS
May 18, 2022
in Data Science, Leading Stories
0 0
0
ConfigParser – manage user-editable settings for your Python programs

User-configurable settings are important for big applications. They make your application more user-friendly and improve the efficiency of your application.

But you may be curious, where and how to store those configurations?

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

Here I am gonna introduce you ConfigParser, one of the standard libraries in Python 3, which is used to save settings for your Python applications.

What exactly is ConfigParser? 🤔

ConfigParser is a Python 3 standard library (You can install it on Python2 by doing pip install configparser) that implements a basic configuration language for Python programs.
The file format used by ConfigParser is INI file, which is similar to the format used by older versions of Microsoft Windows.

The configuration files are organized into sections, and each section can contain name-value pairs for configuration data. The section names are delimited with [] characters. The pairs are separated either with : or =. Comments start either with # or with ;.

What does a ConfigParser INI file look like? 🧐

I think the example from the official docs is the perfect one:

[Simple Values]
key=value
spaces in keys=allowed
spaces in values=allowed as well
spaces around the delimiter = obviously
you can also use : to delimit keys from values

[All Values Are Strings]
values like this: 1000000
or this: 3.14159265359
are they treated as numbers? : no
integers, floats and booleans are held as: strings
can use the API to get converted values directly: true

[Multiline Values]
chorus: I'm a lumberjack, and I'm okay
    I sleep all night and I work all day

[No Values]
key_without_value
empty string value here =

[You can use comments]
# like this
; or this

# By default only in an empty line.
# Inline comments can be harmful because they prevent users
# from using the delimiting characters as parts of values.
# That being said, this can be customized.

    [Sections Can Be Indented]
        can_values_be_as_well=True
        does_that_mean_anything_special=False
        purpose=formatting for readability
        multiline_values=are
            handled just fine as
            long as they are indented
            deeper than the first line
            of a value
        # Did I mention we can indent comments, too?

That says it all, right?

How to read config from ConfigParser INI file? 📄

This is the sample INI file we are using, name it as configurations.ini:

[DEFAULT]
host=localhost
log=True

[MySQL]
PORT=4000
user=john
passwd=IDontwannatellyouhehe123

[Postgresql]
user=peter
PORT=3000
passwd=AnotherPasswd22223

First, we need to import the ConfigParser module, create a ConfigParser object, and read from an INI file:

import configparser

configs = configparser.ConfigParser()
configs.read('configurations.ini')

Now the configs are initialized as an object. Let’s see how we can the values in it:

# Get a value from a section
configs['Postgresql']['user'] # returns: 'peter'

# Assign it to variable
user = configs['Postgresql']['user']
print(user) # returns: 'peter'

To check what sections do we have in the INI file, do:

# List all sections in the INI file
configs.sections() # returns: ['MySQL', 'Postgresql']

# See specific section is in the INI file
'MySQL' in configs # returns: True
'NotExistingSection' in configs # returns: False

To see all value names in a section:

for key in config['MySQL']:
    print(key)
# Returns: 
# port
# user
# passwd
# host
# log

You can also generate a dictionary for values in a section:

configs.items('MySQL') # returns: [('host', 'localhost'), ('log', '1'), ('port', '4000'), ('user', 'john'), ('passwd', 'IDontwannatellyouhehe123')]

Now you may confuse – why did the MySQL section contains host and logvalue? Are you having a typo there?

No no no, that is magic in ConfigParser, the values are in the DEFAULT section (Note that section titles are cAsE-sEnSiTiVe), which is used to provide default values for all other sections.

Well, another thing you may notice is, why the PORT value is printed in lowercase letters?

That’s because value names are case-insensitive, and all of them are stored in lowercase letters.

One last note here: ALL values in ConfigParser are stored as strings.
Therefore, you will need to convert them manually if you want them to be some other data types. There are inbuilt functions for this:

configs['MySQL'].get_boolean('log') # returns: True
configs['MySQL'].get_int('port') # returns: 4000
configs['MySQL'].get_float('port') # returns: 4000.0

Tips for get_boolean() – the method is case-insensitive and recognizes Boolean values from ‘yes’/’no’, ‘on’/’off’, ‘true’/’false’, and ‘1’/’0′.

OK… Now I know how to read the configs, but how to write configs to the INI file? ✍

It’s SUPER easy, just assign them any string you want them to be, and write to the configurations.ini!

Here you go 😎:

# Assign the values you want 
configs['MySQL']['user']='sam'

# Or you can use the `set` method
configs.set(section='Postgresql', option='log', value='False')

# Write it to the file
with open('configurations.ini', 'w') as configfile:
    configs.write(configfile)

Done! Now your settings are written in configurations.ini.

What if I want a new section or change the name of a section?

You can use add_section to create a new section, like this:

configs.add_section('New Section Name')

Well, there is no way to directly rename a section. But what you can do is create a new section, copy values to the new section and delete the old section.

# Create a new section
configs.add_section('New Section')

# Copy values to the new section
for item in configs.items('MySQL'):
    configs.set('New Section', item[0], item[1])

# Delete the new section
configs.remove_section('MySQL')

Thanks for reading! 😎

Source: cycool29
Via: dev.to
Tags: ConfigParser - manage user-editable settings for your Python programs
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version