How do you get the encoding of the terminal from within a python script?
You can call locale.getdefaultlocale()
and use the second part of the tuple.
See more here (Fedora wiki entry explaining the why's and how's of the default encoding in Python)
The function locale.getpreferredencoding()
also seems to do the job.
It returns the Python encoding string which you can directly use like this:
>>> import locale
>>> s = b'123\n'
>>> enc = locale.getpreferredencoding()
>>> s.decode(enc)
'123\n'
sys.stdout.encoding
will give you the encoding of standard output. sys.stdin.encoding
will give you the encdoing for standard input.