このソリューションは、元の文字列で\u
のすべてのインスタンスは、Unicodeエスケープであることを前提としています
def unescape(in_str):
"""Unicode-unescape string with only some characters escaped."""
in_str = in_str.encode('unicode-escape') # bytes with all chars escaped (the original escapes have the backslash escaped)
in_str = in_str.replace(b'\\\\u', b'\\u') # unescape the \
in_str = in_str.decode('unicode-escape') # unescape unicode
return in_str
...または1つのラインで...
def unescape(in_str):
"""Unicode-unescape string with only some characters escaped."""
return in_str.encode('unicode-escape').replace(b'\\\\u', b'\\u').decode('unicode-escape')
奇妙だ。つまり、 'r'сло\u0301во''と入力すると、Pythonインタプリタはそのエスケープを対応する文字に変換し、文字列には実際に'слово'が含まれます。あなたのコードページに応じて、*別の方法でそれを*コンソールに表示するかもしれません。 – roeland