How to: Declare encoding UTF-8 in python

References: 

http://stackoverflow.com/questions/12238307/declaring-encoding-in-python

http://stackoverflow.com/questions/3170211/why-declare-unicode-by-string-in-python

Following explanations are excerpted from above links, this is only used for knowledge sharing:)

Put:

# -*- coding: UTF-8 -*-

as the first line of the file (or second line if using *nix) and save the file as UTF-8.

If you're using Python 2, use Unicode string literals (u"..."), for example:

means = u"a ، b ، c, myCompany™"
lst = means.split(u"،")

When you specify # -*- coding: utf-8 -*-, you're telling Python the source file you've saved is utf-8. The default for Python 2 is ASCII (for Python 3 it's utf-8). This just affects how the interpreter reads the characters in the file.

In general, it's probably not the best idea to embed high unicode characters into your file no matter what the encoding is; you can use string unicode escapes, which work in either encoding.


When you declare a string with a u in front, like u'This is a string', it tells the Python compiler that the string is Unicode, not bytes. This is handled mostly transparently by the interpreter; the most obvious difference is that you can now embed unicode characters in the string (that is, u'u2665' is now legal). You can use from __future__ import unicode_literals to make it the default.

This only applies to Python 2; in Python 3 the default is Unicode, and you need to specify a b in front (like b'These are bytes', to declare a sequence of bytes).

原文地址:https://www.cnblogs.com/cindy-hu-23/p/4317592.html