Search code examples
pythonescapingspecial-charactersconfigparser

read escaped character \t as '\t' instead of '\\t' Python ConfigParser


I have a config.ini file containing delimiter = \t which i now want to read using the python3 ConfigParser. However, the resulting string is '\\t' instead of '\t' which breaks my program. Is there a more elegant option to solve this problem instead of just manually stripping the extra '\' from every variable containing an escaped character? I cannot find an option for ConfigParser().read() to not escape the backslash it finds in the file.


Solution

  • Python3 has a 'unicode_escape' codec.

    r"a\tb".decode('unicode_escape')
    
    'a\tb'
    

    Sources:

    https://bytes.com/topic/python/answers/37952-escape-chars-string

    how do I .decode('string-escape') in Python3?