Python encoding format

Source: Internet
Author: User

Python encoding format

Python encoding summary:

1) First, python has two formats of strings, str and unicode. unicode is equivalent to bytecode and can be used across platforms.

Unicode (), u, and str. decode can be used to convert str to unicode.

Unicode is converted to str. If there is a Chinese character, the encode is generally used.

2). If the Code contains Chinese characters, We will generally add "# coding = UTF-8". What is the function of this? It is generally as follows:

Therefore, when we fill in the coding terminal, use s = "Chinese". In fact, type (s) is a str, which has encoded unicode into str in UTF-8 format.

Second, if we use s = u 'Chinese' in the code, it is equivalent to decoding str into unicode using UTF-8.

1 # coding = UTF-8 2 _ author _ = 'lenovo '3 4 a = 'Chinese' 5 print a, type (a) 6 7 a2 = unicode (, "UTF-8") 8 print a2, type (a2) 9 10 a3 = u 'Chinese' 11 print a3, type (a3)

The output is as follows, which indicates that the declaration header is used to automatically convert unicode to UTF-8 and specify UTF-8 when u is used:

 

3) If unicode is added to unicode, it does not actually work.

1 a = 'Chinese' 2 a2 = unicode (a, "UTF-8") 3 print a2, type (a2) 4 5 a3 = unicode (a2) 6 print a3, type (a3)

4) u, unicode () and str. decode are equivalent. Both can convert str to unicode. However, decode cannot be called consecutively like unicode ().

1 a = 'Chinese' 2 a2 = unicode (a, "UTF-8") 3 a3 =. decode ("UTF-8") 4 print a2, type (a2) 5 print a3, type (a3)

5) when the key stored in map is Chinese, str can be converted to unicode, so that cross-platform

1 a = 'Chinese' 2 map = {} 3 map [. decode ('utf-8')] = 34 map [unicode ('T')] = 45 print map6 print map [u 'China'] 7 print map ['T']

6) If an ascii code is used, unicode is equivalent to str, that is, unicode ('T') = 'T'

7) during file operations, open (filename) requires the file format to be consistent with the encoding header, so that the str type is read.

If they are inconsistent, you can call io. open () and specify the encoding. After reading this code, it is unicode.

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.