On 24 March 2017 at 15:41, Ryan Gonzalez <rym...@gmail.com> wrote:
> Recently, I was working on a Windows GUI application that ends up running
> ffmpeg, and I wanted to see the command that was being run. However, the
> file name had a Unicode character in it (it's a Sawano song), and when I
> tried to print it to the console, it crashed during the encode/decode. (The
> encoding used in cmd doesn't support Unicode characters.)
>
> The workaround was to do:
>
>
> print(mystring.encode(sys.stdout.encoding,
> errors='replace).decode(sys.stdout.encoding))
>
>
> Not fun, especially since this was *just* a debug print.
>
> The proposal: why not add an 'errors' argument to print? That way, I
> could've just done:
>
>
> print(mystring, errors='replace')
>
>
> without having to worry about it crashing.

When I've hit issues like this before, I've written a helper function:

def sanitise(str, enc):
    """Ensure that str can be encoded in encoding enc"""
    return str.encode(enc, errors='replace').decode(enc)

An errors argument to print would be very similar, but would only
apply to the print function, whereas I've used my sanitise function in
other situations as well.

I understand the attraction of a dedicated "just print the best
representation you can" argument to print, but I'm not sure it's a
common enough need to be worth adding like this.

Paul
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to