Python mileage limiter in config file using ConfigParser

I would like to avoid ":" and / or "=" as the name in the config file. Does anyone know how to achieve this? I try the backslash "\", it doesn't work.

+3


source to share


1 answer


If you are using Python 3 you don't need. Take a look at the Python Docs section on Customizing Parser Behavior . By default, configparser uses ":" and "=" as delimiters, but you can specify different delimiters when creating the configparser object:

import configparser

parser = configparser.ConfigParser(delimiters=('?', '*'))

      



In this example, the default separators have been replaced with a question mark and an asterisk. You can change the delimiters to whatever characters you want, which won't conflict with the information needed to be placed in the config file.

The above method will only work for Python 3 as the Python 2 ConfigParser is hardcoded to recognize the same characters and colons as separators. As per this SO question, there is a fallback configurator available for the 2.7 built-in translator at https://pypi.python.org/pypi/configparser . See if this works for you.

+5


source







All Articles