Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Last active August 29, 2015 14:05
Show Gist options
  • Save IQAndreas/c6982809b0f8b269e6d8 to your computer and use it in GitHub Desktop.
Save IQAndreas/c6982809b0f8b269e6d8 to your computer and use it in GitHub Desktop.
Get the encoding of the console the script is currently running in (plus a few other lines for debugging)
#!/usr/bin/env python3
import sys, locale;
print(sys.stdout.encoding);
print(locale.getpreferredencoding());
# http://www.macfreek.nl/memory/Encoding_of_Python_stdout#StreamWriter_Wrapper_around_Stdout
if sys.stdout.encoding != 'UTF-8':
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')
print(u"\u201C" + "Hello world!" + u"\u201D")
print("\u201CHello world!\u201D")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment