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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With