Created
February 22, 2018 00:20
-
-
Save ilikepi/e51fdf72701d3f3f839323d1cbb12fcd to your computer and use it in GitHub Desktop.
Python Unicode output tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import codecs | |
import locale | |
unicode_string = u' \\u2013: \u2013' | |
print 'sys.stdout.encoding: %(enc)s' % {'enc': sys.stdout.encoding} | |
try: | |
print 'trying `print`...' | |
print unicode_string | |
except UnicodeEncodeError: | |
print ' UnicodeEncodeError' | |
try: | |
print 'trying default `sys.stdout.write`...' | |
sys.stdout.write(unicode_string) | |
except UnicodeEncodeError: | |
print ' UnicodeEncodeError' | |
try: | |
# https://wiki.python.org/moin/PrintFails | |
print 'trying StreamWriter `sys.stdout.write`...' | |
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout) | |
sys.stdout.write(unicode_string) | |
except UnicodeEncodeError: | |
print ' UnicodeEncodeError' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment