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.
- Introduction to Python system files
- How to correctly use Python Functions
- Detailed introduction and analysis of Python build tools
- Advantages of Python in PythonAndroid
- How to Use the Python module to parse the configuration file?