[melbourne-pug] Unicode encode problem

Mike Dewhirst miked at dewhirst.com.au
Tue Sep 22 11:51:53 CEST 2015


I'm using python 3.4 and having unicode difficulties which I suspect are 
due to Python 3.x being rigorous.

How do I encode particular chars?

I have a utility which swaps subscripted unicode chars for plain integer 
chars ... like this:

if char == "2":
     char = "\u2082"
elif char == "3":
     char = "\u2083"
elif char == "4":
     char = "\u2084"
elif char == "5":
     char = "\u2085"
elif char == "6":
     char = "\u2086"
elif char == "7":
     char = "\u2087"
elif char == "8":
     char = "\u2088"
elif char == "9":
     char = "\u2089"
elif char == "1":
     char = "\u2081"
elif char == "0":
     char = "\u2080"

Django is smart enough to protect me from my unicode ignorance because 
the error only occurs when I enable logging (which is plain Python) and 
a sample log message is:

Message: '(0.000) INSERT INTO "substance_synonym" ("substance_id", 
"name", "jurisdiction_code", "display", "section", "created", 
"modified", "modified_by_id") VALUES (1, \'C\u2083H\u2087O\u2088\', 
NULL, true, 3, \'2015-09-22 09:23:46.951070+00:00\', \'2015-09-22 
09:23:46.951070+00:00\', NULL) RETURNING "substance_synonym"."id"; 
args=(1, \'C\u2083H\u2087O\u2088\', None, True, 3, \'2015-09-22 
09:23:46.951070+00:00\', \'2015-09-22 09:23:46.951070+00:00\', None)'

--- Logging error ---
Traceback (most recent call last):
   File "C:\Python34\Lib\logging\__init__.py", line 980, in emit
     stream.write(msg)
   File "C:\Users\mike\env\xxex3\lib\encodings\cp1252.py", line 19, in 
encode
     return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2083' in 
position 293: character maps to <undefined>

Thanks for any hints

Mike


More information about the melbourne-pug mailing list