问题
I am attempting to write a python script that converts a .properties file (as read by Ant), and converts the result into a dictionary, mapping keys to values.
As part of the process (using configparser.RawConfigParser), I discovered that .properties files have their values escaped, and I decided to follow the top result to try and de-escape them (see How do I un-escape a backslash-escaped string in python?). As I am using python 3 (specifically, python 3.6), I use value = value.encode('utf-8').decode('unicode_escape')
to un-escape the string.
This was all working well, until I came across a Windows path, which was escaped like r'C\:\\Path\\to\\something'
, note the colon is escaped. While attempting to resolve this, I decided to consult the documentation, only to find that in the latest python (3.7), re.escape does NOT escape :
. However, python 3.6 does. This leads me to start questioning language compatibility:
Does
r'\:'.encode('utf-8').decode('unicode_escape')
return':'
orr'\:'
in python 3.7?How can I handle de-escaping colons in python 3, without needing to worry about version? If I follow up the decode with
value = value.replace(r'\:', ':')
, I don't want it to erroneously replace values.Is this a bug that was fixed in python 3.7?
来源:https://stackoverflow.com/questions/51903640/proper-way-to-de-escape-a-string-with-an-escaped-colon-in-python-3-6