Welcome, guest | Sign In | My Account | Store | Cart

A replacement for "print" that will safely handle unicode conversion.

Python, 8 lines
1
2
3
4
5
6
7
8
def _safe_print(u, errors="replace"):
    """Safely print the given string.
    
    If you want to see the code points for unprintable characters then you
    can use `errors="xmlcharrefreplace"`.
    """
    s = u.encode(sys.stdout.encoding or "utf-8", errors)
    print(s)
Created by Trent Mick on Fri, 2 Jan 2009 (MIT)
Python recipes (4591)
Trent Mick's recipes (28)

Required Modules

  • (none specified)

Other Information and Tasks