Given this example, I get the error that follows:
print u'\2033'.translate({2033:u'd'})
C:\Python26\lib\encodings\cp437.pyc in encode(self, input, errors)
10
11 def encode(self,input,errors='strict'):
---> 12 return codecs.charmap_encode(input,errors,encoding_map)
13
14 def decode(self,input,errors='strict'):
UnicodeEncodeError: 'charmap' codec can't encode character u'\x83' in position 0
Try this instead:
Since you used
u'\2033'instead ofu'\u2033', The result was two characters:u'\203'+u'3'. Trying to print this gave an exception because your terminal’s encoding doesn’t support the characteru'\203'(which is the same asu'\x83').Also note the difference between
2033and0x2033in the dictionary: The\uxxxxescape sequence takes its value in hexadecimal, so you’d need0x2033to match it.Regarding your question’s title,
string.translate(thetranslatefunction in thestringmodule) doesn’t support a dictionary as a parameter, but calling.translateon the unicode string itself (as you did in the question body) works.