Proper way to de-escape a string with an escaped colon “:” in python 3.6

 ̄綄美尐妖づ 提交于 2019-12-24 19:37:29

问题


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?

来源:https://stackoverflow.com/questions/51903640/proper-way-to-de-escape-a-string-with-an-escaped-colon-in-python-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!