Conversion of character encodings
Coding problem has always been a difficult problem to understand, the conversion of the inexplicable conversion, the results of the program can be correctly output, and finally set aside a little time to begin to understand the thorny problem.
Python has two types of strings, str, Unicode, both of which are basestring subclasses
STR is a string of bytes, while Unicode is the true meaning of strings
STR can be converted to Unicode by the Decode () function;
Unicode can be converted to STR via the Encode () function.
Unicode is a uniform encoding that supports all text, but is generally used only as an internal representation of text, where files, Web pages (also files), screen input and output, and so on are required to use specific external codes such as GBK, UTF-8 , etc.
Unicode is a binary encoding in which all utf-8 and GBK encodings are translated through Unicode encoding, and the utf-8 and GBK encodings cannot be converted directly between Unicode.
#假如我知道一串编码是用utf 8, how to turn into GBK? u = S.decode ("Utf-8") # converts utf-8 str to unicodeg = U.encode (' GBK ') # converts Unicode to STR, encoded as gbk# or S.decode (' utf-8 '). Encode (' GBK ')
Convert to a graphic
How do I see the encoded format of a character?
Download third-party module Chardet
Import Chardet s = ' Kanji '
>>> {' confidence ': 0.99, ' encoding ': ' Utf-8 '} #chardet. Detect () returns the dictionary, where confidence is the detection accuracy, Encoding is a coded form
Python character encoding