What is the difference between sys.stdout.encoding, locale.getpreferredencoding(), and sys.getdefaultencoding()?

564 views Asked by At

I am new to python and really confused about this encoding stuff. So far, I've read about the following types of "encoding":

import sys
import locale

print (sys.stdout.encoding)
print (locale.getpreferredencoding())
print (sys.getdefaultencoding())

output:

utf8
cp1252
utf-8

What is the difference?

1

There are 1 answers

1
OShadmon On BEST ANSWER

Simply put, encoding is way your data is stored inside memory.The different ways, allow for more characters, and information. For an in-depth explanation you are more than welcome to read either http://kunststube.net/encoding/, or Wikipedia

In python you can change the way things are stored by either physically calling an encoding type, or with the use of any of the coding function.

For your environment of python3.x, there is no difference between sys.stdout.encoding and sys.getdefaultencoding(). They both use the 8-bite code units (most standard). While the preferred encoding,locale.getpreferredencoding() (cp1252), is the windows version of latin1.

Note that if you would like to get quick feedback with any method/function, you can always use the help command.

Example:

>>> import locale
>>> help(locale.getpreferredencoding)

Output:

Help on function getpreferredencoding in module locale:

getpreferredencoding(do_setlocale=True)
    Return the charset that the user is likely using,
    according to the system configuration.
(END)