Illustration of the Python language

Source: Internet
Author: User

The Python language also indicates that the Shell of IDLE supports two formats of Chinese strings: the "str" Object encoded by GBK, and the UNICODE object encoded by unicode, I also use a UTF-8 when connecting, why is the retrieved text content a UNICODE-encoded unicode object )? Is this the setting of MySQLdb library?

When editing a file in Notepad under a window, if it is saved as UNICODE or UTF-8, two bytes "\ xFF \ xFE" and three bytes "\ xEF \ xBB \ xBF" are added at the beginning of the file respectively ". You may encounter problems when reading data, but the processing of these characters varies with different environments.

Open a file in UTF-8 format and read the UTF-8 string, and then decode it into a unicode object. However, the added three characters are converted to a unicode character. The data value of the character is \ xFF \ xFE ". This character cannot be printed. Skip this character during encoding.

After opening a file in unicode format, the obtained string is correct. This applies to UTF-16 decoding, can get the correct unicdoe object, can be used directly. The extra character will be filtered out during conversion.

After an ansi file is opened, it can be used directly without filling characters. Conclusion: there is no problem in reading and writing a file generated using python, but when processing a text file generated by notepad, if the file may be non-ansi encoded, consider how to fill in characters.

I have just been familiar with the Python language. The database I use is mysql. During the insert and search operations, if the character encoding used in the runtime environment is inconsistent with that in mysql, it may cause runtime errors. Of course, this is the same as what we saw above.

The running environment is not a key factor, but the encoding method of the query statement. If the query string is converted to the default character encoding of mysql during each query operation, no problem will occur. But writing code in this way is too painful.

The phase plane is used to compare the two methods:


In addition, in the shell of Python, do not assign values to attributes using u'chinese. As discussed above, the resulting unicode string is incorrect.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.