Python Coding issues

Source: Internet
Author: User

The following applies to Python version 2.7.

1. About ASCII Unicode Utf-8:

ascii:127 number A--Z--1221 bytes represents one character (255)

Unicode: typically two bytes (65535) represent a character, and an uncommon character may represent a character in four bytes. The ASCII binary representation in Unicode is preceded by 0. (The encoding used by the computer is Unicode)

If a document is full of English characters, the use of Unicode can result in a waste of memory, resulting in utf-8 (variable length encoding).

ASCII consumes one byte of memory in Utf-8, while Chinese occupies three bytes.

2. Unicode encoding is used in the computer, and when it comes to hard disk storage, Unicode is converted to utf-8 storage. For example: Notepad for storage.

Similarly, on a Web page, the Unicode data is converted to utf-8 displayed on the browser.

ASCII-to-digital conversion in 3.Python

>>> Ord ('A')+ >>> chr  A'

4.Python added support for Unicode, which is represented by a string in Unicode u‘...‘

Print u'  Chinese '  >>> u '  u ' ' \u4e2d '

5. u‘xxx‘ Convert to UTF-8 encoding ‘xxx‘ encode(‘utf-8‘) method: (Unicode to Utf-8)

 >>> u " abc  " . Encode ( " utf-8  Span style= "COLOR: #800000" > " )  "  Span style= "COLOR: #800000" >abc   " >>> u  "  Chinese   ". Encode ("   Utf-8   " )   '  

6. In turn, convert the string represented by the UTF-8 encoding ‘xxx‘ to a Unicode string u‘xxx‘ using the decode(‘utf-8‘) method: (Utf-8 converted to Unicode )

>>>'ABC'. Decode ('Utf-8') U'ABC'>>>'\xe4\xb8\xad\xe6\x96\x87'. Decode ('Utf-8') U'\u4e2d\u6587'>>>Print '\xe4\xb8\xad\xe6\x96\x87'. Decode ('Utf-8') Chinese

7.Python file header Specifies the encoding format to read (in case the file contains Chinese, and the Chinese string must be a Unicode string)

# !/usr/bin/env python # -*-coding:utf-8-*-

Python Coding issues

Related Article

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.