Трагически, мой любимый мультфильм слишком стар, чтобы быть в межсетях.
Это от Вычислительной техники. (Помните Вычислительную технику?) Человек сидит за столом. Существуют биты электроники везде. Щебень усыпан в каждый угол комнаты. Его поверхность и волосы подпалились.
Он думает, что "Это никогда не делало это прежде".
>>> s = u'Rafa\xc5\x82' >>> s.encode('raw_unicode_escape').decode('utf-8') u'Rafa\u0142' >>>
Ага, это было весело!
>>> original = "Rafa\xc3\x85\xc2\x82"
>>> first_decode = original.decode('utf-8')
>>> as_chars = ''.join([chr(ord(x)) for x in first_decode])
>>> result = as_chars.decode('utf-8')
>>> result
u'Rafa\u0142'
Итак, вы выполняете первое декодирование, получая строку Unicode, где каждый символ фактически является байтовым значением UTF-8. Вы переходите через целочисленное значение каждого из этих символов, чтобы вернуться к подлинной строке UTF-8, которую затем декодируете как обычно.
>>> weird = u'Rafa\xc5\x82'
>>> weird.encode('latin1').decode('utf8')
u'Rafa\u0142'
>>>
latin1 is just an abbreviation for Richie's nuts'n'bolts method.
It is very curious that the seriously under-described raw_unicode_escape
codec gives the same result as latin1
in this case. Do they always give the same result? If so, why have such a codec? If not, it would preferable to know for sure exactly how the OP's client did the transformation from 'Rafa\xc5\x82'
to u'Rafa\xc5\x82'
and then to reverse that process exactly -- otherwise we might come unstuck if different data crops up before the double encoding is fixed.