Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminating spaces between equal signs in ConfigParser - Python [duplicate]

I got an excellent answer from this post, that covered everything I wanted.

[Box]
box.active = false
resolution_tracker.active = true
box.api_key = 
box.api_secret = 
box.job_interval = 480
box.max_attempts = 6
box.users = 

[Google]
google.active = true
google.job_interval = 480
google.users = <useremail>
google.key_file_name = <key_file>
google.service_account_id = <account_id>

However, the question still lingering around is how to remove the spaces in the equal assignments. For example:

box.active = false

Must be

box.active=false

That's it, I want to remove the white spaces between the =. The .properties file is generated with ConfigParser in Python, but it seems those white spaces are creating problems. Of course I can use other things in order to remove those white spaces, but is there a more elegant solution using StringIO, ConfigParser or any other of the Python Libraries?

** Edit ** This question isn't a duplicate because we are trying to find an easier way to remove the white spaces around the = using the API, instead of rewriting the ConfigParser class.

like image 230
cybertextron Avatar asked Oct 30 '15 16:10

cybertextron


People also ask

Which of the following code returns the list of all sections available in config file?

The configparser module has ConfigParser class. It is responsible for parsing a list of configuration files, and managing the parsed database. Return all the configuration section names.

What is Configparser Configparser ()?

Source code: Lib/configparser.py. This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files.


1 Answers

ConfigParser.write() has a flag for that

#!/usr/bin/env python3
import sys
from configparser import ConfigParser
from io import StringIO


CONFIG = '''
[Box]
box.active = false
resolution_tracker.active = true
box.api_key = 
box.api_secret = 
box.job_interval = 480
box.max_attempts = 6
box.users = 

[Google]
google.active = true
google.job_interval = 480
google.users = <useremail>
google.key_file_name = <key_file>
google.service_account_id = <account_id>
'''

parser = ConfigParser()
parser.readfp(StringIO(CONFIG))
parser.write(sys.stdout, space_around_delimiters=False)

output:

[Box]
box.active=false
resolution_tracker.active=true
box.api_key=
box.api_secret=
box.job_interval=480
box.max_attempts=6
box.users=

[Google]
google.active=true
google.job_interval=480
google.users=<useremail>
google.key_file_name=<key_file>
google.service_account_id=<account_id>
like image 142
decltype_auto Avatar answered Nov 15 '22 00:11

decltype_auto