Python escape delimiter in configuration file using ConfigParser

后端 未结 1 909
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 04:01

I\'d like to escape \":\" and/or \"=\" as the name in a configuration file. Does anyone know how to achieve this? I try backslash \"\\\", it does not work.

相关标签:
1条回答
  • 2021-01-20 04:23

    If you're using Python 3, you don't need to. Look at the Python docs section on Customizing Parser Behavior. By default, configparser uses ":" and "=" as delimiters, but you can specify different delimiters when you create the configparser object:

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

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

    The above listed method will only work for Python 3, as the Python 2 ConfigParser is hard-coded to recognize equal signs and colons as delimiters. According to this SO question, there is a backported configparser available for the 2.7 intepreter at https://pypi.python.org/pypi/configparser. See if that will work for you.

    0 讨论(0)
提交回复
热议问题