1

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:

  1. Does r'\:'.encode('utf-8').decode('unicode_escape') return ':' or r'\:' in python 3.7?

  2. 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.

  3. Is this a bug that was fixed in python 3.7?

Casey Kuball
  • 7,717
  • 5
  • 38
  • 70
  • 1
    `r` has nothing to do with `re.escape`. One is a prefix for string literals in your source code, and means that backslashes in that literal are backslashes, not escape sequences. The other takes a string value and escapes anything needed to use that string value in a literal expression without treating any of its characters as special regex characters. – abarnert Aug 17 '18 at 23:18
  • 1
    @abarnert, I know, it stands for 'raw', I'm merely using raw strings to easily show my meaning that every backslash is escaped. However, there is no standard library way of de-escaping a string , and re-escape seems to be the most related thing in the standard library, and I thought there could be consistency there (perhaps not, could be part of the answer). – Casey Kuball Aug 18 '18 at 14:28

0 Answers0