Python coding error prevention

I have scripts that print messages by the logging system or sometimes print commands. On the Windows console, I get error messages like

Traceback (most recent call last): File "C:\Python32\lib\logging\__init__.py", line 939, in emit stream.write(msg) File "C:\Python32\lib\encodings\cp850.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 4537:character maps to <undefined> 

Is there a general way to do all the encodings in the logging system, print commands, etc. fail safe (ignore errors)?

+6
source share
1 answer

The problem is that your terminal / shell (cmd, like you on Windows) cannot print every Unicode character.

You may str.encode encode your lines with the errors argument of the str.encode method. For example, can you replace unsupported characters ? by setting errors='replace' .

 >>> s = u'\u2019' >>> print s Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\encodings\cp850.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can\'t encode character u'\u2019' in position 0: character maps to <undefined> >>> print s.encode('cp850', errors='replace') ? 

See the documentation for other parameters.

Change If you want a generic logging solution, you can subclass StreamHandler :

 class CustomStreamHandler(logging.StreamHandler): def emit(self, record): record = record.encode('cp850', errors='replace') logging.StreamHandler.emit(self, record) 
+9
source

Source: https://habr.com/ru/post/918172/


All Articles