Backport PR #4581.

Fixing nbconvert writing unicode characters to redirected stdout.