The important of encoding

Getting the encoding right is crucial for correctly interpreting the contents of a text file, so we want to labor the point a bit. Python can't reliably determine the encoding of a text file, so it doesn't try. Yet without knowing the encoding of a file, Python can't properly manipulate the data in the file. That's why it's critical that you tell Python which encoding to use.

If you don't specify an encoding Python will use the default from sys.getdefaultencoding(). In our case, the default encoding is 'utf-8':

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'

Always remember, though, that there's no guarantee that the default encoding on your system is the same as the default encoding on another system with which you wish to exchange files. It's better for all concerned to make a conscious decision about the text-to-bytes encoding by specifying it in your calls to open(). You can get a list of supported text encodings in the Python documentation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset