The relationship between Decode,encode and Unicode in Python (Encode usage, decode usage)

Source: Internet
Author: User
Tags in python

Turn from: http://blog.csdn.net/flyingtimeice/article/details/4283145

Beginners Python, encountered a lot of coding problems, write down to avoid the future and forget, a lot of things do not understand, are belong to superficial understanding, disorderly conclusion, but thought to take out can have enthusiastic students point out the wrong, then thick shameless ...
First you need to understand that there are two kinds of strings in Python (which, strictly speaking, don't seem to be called). One is the normal str object (each character is represented in 8bits) and the other is a Unicode string that can be converted to each other.
First open Pyshell and enter a piece of code.

Python code

[python] view plain copy >>> a = "I" >>> B = Unicode (A, "gb2312") >>> a.__ class__ <type ' str ' > >>> b.__class__ <type ' Unicode ' > >>>

See, there are two kinds of strings.
Then come python code [python] view plain copy >>> a '/xce/xd2 ' >>> b u '/u6211 '

Variable A is two characters and B is a Unicode character.
For both of these strings, the Python document-->languagereference-->datamodel-->the standard type hierarchy-->sequences, There are some strings,unicode descriptions.
As for Python code [python] view plain copy >>> z = u "i" >>> #这种代码, in fact nothing. >>> z.__class__ <type ' Unicode ' > >>> z u '/xce/xd2 '

You see, this strange thing ...
Later in the Windows XP, Pure Python command line tried, the conclusion is different, Z's result turned into U '/u6211 ', here should not be tested in Pyshell, there seems to be a lot of problems have not been understood clearly


let's look at Encode,decode .
What situation with encode, what situation is decode again, just at the beginning is always get dizzy. In fact, the English names of various local character sets arecodedCharacter Set, to be converted tocoded, must be encode, the same, from the inside of the solution should also be called decode ...
Decode is the conversion of other encodings to Unicode, equivalent to Unicode functions; encode converts Unicode-encoded strings to specific encodings. To continue in the Pyshell:
A is a str type, so you can use encode to make an error. The default encoding conversion is invoked for system encoding when the print output is used. Python code[Python]  View plain copy >>> a.decode ("gb2312")        u '/u6211 '         >>> print a.decode ("gb2312")         I        >>> a.encode ("gb2312")         traceback  (most recent call last):          file  "<input>", line 1, in ?       unicodedecodeerror:  ' ASCII '  codec can ' t decode byte 0xce in position  0: ordinal not in range (128)   

B is a Unicode type that needs to be encode (encoded) into a system-coded python code [python] view plain copy >>> print b.encode ("gb2312") I >>> b.encode ("gb2312") '/xce/xd2 ' >>> b.decode ("gb2312") Traceback (Most RE       Cent call last): File "<input>", line 1, in? Unicodeencodeerror: ' ASCII ' codec can ' t encode character U '/u6211 ' in position-not in range (128)

The default encode and decode in Python are strict mode, so they throw the error directly, and Java is the default replace mode, so you often see a bunch of????? when working with the servlet
Passing in the second argument at decode errors as ' replace ' can be the same as Java, but it's always unsuccessful, and I don't know why

MySQLdb The encoding problem of connecting to the database
tried for a long time, either by specifying charset= ' UTF8 ' at connect, or by using Set_character_set (), or by executing the ' set NAMES UTF8 ", Trace to Character_set_name () method return is latin1 ... Again with the code, it seems to run to the MYSQL-API, anyway the final solution is very simple, is to use the following way to execute, and not to spell SQL statements ... python code [python]  

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.