Python character encoding

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.