sys
module is sys.getdefaultencoding()
locale.getpreferredencoding()
sys.stdout, sys.stderr
# -*- coding: utf-8 -*- >>> import sys >>> import locale >>> print sys.getdefaultencoding() ascii >>> print locale.getpreferredencoding() # linux UTF-8 >>> print locale.getpreferredencoding() # win32/rus cp1251 # : >>> print sys.stdout.encoding # linux UTF-8 >>> print sys.stdout.encoding # win32 cp866
UnicodeEncodeError/UnicodeDecodeError
.
#!/usr/bin/env python # -*- coding: utf-8 -*- # ============== # Main script file # ============== import sys reload(sys) sys.setdefaultencoding('utf-8') # import locale sys.setdefaultencoding(locale.getpreferredencoding()) # ...
print
th. When you go to the output to the screen through logging
everything is broken.
sys.setdefaultencoding(sys.stdout.encoding or sys.stderr.encoding)
①
), “kindly” added by the author to any title, again we get UnicodeEncodeError
! import sys import codecs sys.stdout = codecs.getwriter('cp866')(sys.stdout,'replace')
UINT GetOEMCP(void); // OEM UINT GetANSICP(void); // ANSI
# -*- coding: utf-8 -*- import sys import codecs def setup_console(sys_enc="utf-8"): reload(sys) try: # win32 if sys.platform.startswith("win"): import ctypes enc = "cp%d" % ctypes.windll.kernel32.GetOEMCP() #TODO: win64/python64 else: # Linux , , enc = (sys.stdout.encoding if sys.stdout.isatty() else sys.stderr.encoding if sys.stderr.isatty() else sys.getfilesystemencoding() or sys_enc) # sys sys.setdefaultencoding(sys_enc) # , if sys.stdout.isatty() and sys.stdout.encoding != enc: sys.stdout = codecs.getwriter(enc)(sys.stdout, 'replace') if sys.stderr.isatty() and sys.stderr.encoding != enc: sys.stderr = codecs.getwriter(enc)(sys.stderr, 'replace') except: pass # ? - -...
logging.StreamHandler
, to a certain set, when importing automatically replacing the standard StreamHandler.
class ColoredHandler( logging.StreamHandler ): def emit( self, record ): # Need to make a actual copy of the record # to prevent altering the message for other loggers myrecord = copy.copy( record ) levelno = myrecord.levelno if( levelno >= 50 ): # CRITICAL / FATAL color = '\x1b[31;1m' # red elif( levelno >= 40 ): # ERROR color = '\x1b[31m' # red elif( levelno >= 30 ): # WARNING color = '\x1b[33m' # yellow elif( levelno >= 20 ): # INFO color = '\x1b[32m' # green elif( levelno >= 10 ): # DEBUG color = '\x1b[35m' # pink else: # NOTSET and anything else color = '\x1b[0m' # normal myrecord.msg = (u"%s%s%s" % (color, myrecord.msg, '\x1b[0m')).encode('utf-8') # normal logging.StreamHandler.emit( self, myrecord )
print u"\x1b[31;40m- \x1b[0m"
if you suddenly want to pervert.
# -*- coding: utf-8 -*- import sys import codecs import copy import logging #: Is ANSI printing available ansi = not sys.platform.startswith("win") def setup_console(sys_enc='utf-8', use_colorama=True): """ Set sys.defaultencoding to `sys_enc` and update stdout/stderr writers to corresponding encoding .. note:: For Win32 the OEM console encoding will be used istead of `sys_enc` """ global ansi reload(sys) try: if sys.platform.startswith("win"): #... , if use_colorama and sys.platform.startswith("win"): try: # colorama `ansi`, from colorama import init init() ansi = True except: pass class ColoredHandler( logging.StreamHandler ): def emit( self, record ): # Need to make a actual copy of the record # to prevent altering the message for other loggers myrecord = copy.copy( record ) levelno = myrecord.levelno if( levelno >= 50 ): # CRITICAL / FATAL color = '\x1b[31;1m' # red elif( levelno >= 40 ): # ERROR color = '\x1b[31m' # red elif( levelno >= 30 ): # WARNING color = '\x1b[33m' # yellow elif( levelno >= 20 ): # INFO color = '\x1b[32m' # green elif( levelno >= 10 ): # DEBUG color = '\x1b[35m' # pink else: # NOTSET and anything else color = '\x1b[0m' # normal myrecord.msg = (u"%s%s%s" % (color, myrecord.msg, '\x1b[0m')).encode('utf-8') # normal logging.StreamHandler.emit( self, myrecord )
#!/usr/bin/env python # -*- coding: utf-8 -*- from setupcon import setup_console setup_console('utf-8', False) #... # import setupcon setupcon.setup_console() import logging #... if setupcon.ansi: logging.getLogger().addHandler(setupcon.ColoredHandler())
Source: https://habr.com/ru/post/117236/